array.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. 'use strict';
  2. /*!
  3. * Module dependencies.
  4. */
  5. const $exists = require('./operators/exists');
  6. const $type = require('./operators/type');
  7. const MongooseError = require('../error/mongooseError');
  8. const SchemaArrayOptions = require('../options/SchemaArrayOptions');
  9. const SchemaType = require('../schematype');
  10. const CastError = SchemaType.CastError;
  11. const Mixed = require('./mixed');
  12. const arrayDepth = require('../helpers/arrayDepth');
  13. const cast = require('../cast');
  14. const get = require('../helpers/get');
  15. const isOperator = require('../helpers/query/isOperator');
  16. const util = require('util');
  17. const utils = require('../utils');
  18. const castToNumber = require('./operators/helpers').castToNumber;
  19. const geospatial = require('./operators/geospatial');
  20. const getDiscriminatorByValue = require('../helpers/discriminator/getDiscriminatorByValue');
  21. let MongooseArray;
  22. let EmbeddedDoc;
  23. const isNestedArraySymbol = Symbol('mongoose#isNestedArray');
  24. const emptyOpts = Object.freeze({});
  25. /**
  26. * Array SchemaType constructor
  27. *
  28. * @param {String} key
  29. * @param {SchemaType} cast
  30. * @param {Object} options
  31. * @inherits SchemaType
  32. * @api public
  33. */
  34. function SchemaArray(key, cast, options, schemaOptions) {
  35. // lazy load
  36. EmbeddedDoc || (EmbeddedDoc = require('../types').Embedded);
  37. let typeKey = 'type';
  38. if (schemaOptions && schemaOptions.typeKey) {
  39. typeKey = schemaOptions.typeKey;
  40. }
  41. this.schemaOptions = schemaOptions;
  42. if (cast) {
  43. let castOptions = {};
  44. if (utils.isPOJO(cast)) {
  45. if (cast[typeKey]) {
  46. // support { type: Woot }
  47. castOptions = utils.clone(cast); // do not alter user arguments
  48. delete castOptions[typeKey];
  49. cast = cast[typeKey];
  50. } else {
  51. cast = Mixed;
  52. }
  53. }
  54. if (options != null && options.ref != null && castOptions.ref == null) {
  55. castOptions.ref = options.ref;
  56. }
  57. if (cast === Object) {
  58. cast = Mixed;
  59. }
  60. // support { type: 'String' }
  61. const name = typeof cast === 'string'
  62. ? cast
  63. : utils.getFunctionName(cast);
  64. const Types = require('./index.js');
  65. const caster = Types.hasOwnProperty(name) ? Types[name] : cast;
  66. this.casterConstructor = caster;
  67. if (this.casterConstructor instanceof SchemaArray) {
  68. this.casterConstructor[isNestedArraySymbol] = true;
  69. }
  70. if (typeof caster === 'function' &&
  71. !caster.$isArraySubdocument &&
  72. !caster.$isSchemaMap) {
  73. const path = this.caster instanceof EmbeddedDoc ? null : key;
  74. this.caster = new caster(path, castOptions);
  75. } else {
  76. this.caster = caster;
  77. if (!(this.caster instanceof EmbeddedDoc)) {
  78. this.caster.path = key;
  79. }
  80. }
  81. this.$embeddedSchemaType = this.caster;
  82. }
  83. this.$isMongooseArray = true;
  84. SchemaType.call(this, key, options, 'Array');
  85. let defaultArr;
  86. let fn;
  87. if (this.defaultValue != null) {
  88. defaultArr = this.defaultValue;
  89. fn = typeof defaultArr === 'function';
  90. }
  91. if (!('defaultValue' in this) || this.defaultValue !== void 0) {
  92. const defaultFn = function() {
  93. // Leave it up to `cast()` to convert the array
  94. return fn
  95. ? defaultArr.call(this)
  96. : defaultArr != null
  97. ? [].concat(defaultArr)
  98. : [];
  99. };
  100. defaultFn.$runBeforeSetters = !fn;
  101. this.default(defaultFn);
  102. }
  103. }
  104. /**
  105. * This schema type's name, to defend against minifiers that mangle
  106. * function names.
  107. *
  108. * @api public
  109. */
  110. SchemaArray.schemaName = 'Array';
  111. /**
  112. * Options for all arrays.
  113. *
  114. * - `castNonArrays`: `true` by default. If `false`, Mongoose will throw a CastError when a value isn't an array. If `true`, Mongoose will wrap the provided value in an array before casting.
  115. *
  116. * @static options
  117. * @api public
  118. */
  119. SchemaArray.options = { castNonArrays: true };
  120. /*!
  121. * ignore
  122. */
  123. SchemaArray.defaultOptions = {};
  124. /**
  125. * Sets a default option for all Array instances.
  126. *
  127. * ####Example:
  128. *
  129. * // Make all Array instances have `required` of true by default.
  130. * mongoose.Schema.Array.set('required', true);
  131. *
  132. * const User = mongoose.model('User', new Schema({ test: Array }));
  133. * new User({ }).validateSync().errors.test.message; // Path `test` is required.
  134. *
  135. * @param {String} option - The option you'd like to set the value for
  136. * @param {*} value - value for option
  137. * @return {undefined}
  138. * @function set
  139. * @api public
  140. */
  141. SchemaArray.set = SchemaType.set;
  142. /*!
  143. * Inherits from SchemaType.
  144. */
  145. SchemaArray.prototype = Object.create(SchemaType.prototype);
  146. SchemaArray.prototype.constructor = SchemaArray;
  147. SchemaArray.prototype.OptionsConstructor = SchemaArrayOptions;
  148. /*!
  149. * ignore
  150. */
  151. SchemaArray._checkRequired = SchemaType.prototype.checkRequired;
  152. /**
  153. * Override the function the required validator uses to check whether an array
  154. * passes the `required` check.
  155. *
  156. * ####Example:
  157. *
  158. * // Require non-empty array to pass `required` check
  159. * mongoose.Schema.Types.Array.checkRequired(v => Array.isArray(v) && v.length);
  160. *
  161. * const M = mongoose.model({ arr: { type: Array, required: true } });
  162. * new M({ arr: [] }).validateSync(); // `null`, validation fails!
  163. *
  164. * @param {Function} fn
  165. * @return {Function}
  166. * @function checkRequired
  167. * @api public
  168. */
  169. SchemaArray.checkRequired = SchemaType.checkRequired;
  170. /**
  171. * Check if the given value satisfies the `required` validator.
  172. *
  173. * @param {Any} value
  174. * @param {Document} doc
  175. * @return {Boolean}
  176. * @api public
  177. */
  178. SchemaArray.prototype.checkRequired = function checkRequired(value, doc) {
  179. if (SchemaType._isRef(this, value, doc, true)) {
  180. return !!value;
  181. }
  182. // `require('util').inherits()` does **not** copy static properties, and
  183. // plugins like mongoose-float use `inherits()` for pre-ES6.
  184. const _checkRequired = typeof this.constructor.checkRequired == 'function' ?
  185. this.constructor.checkRequired() :
  186. SchemaArray.checkRequired();
  187. return _checkRequired(value);
  188. };
  189. /**
  190. * Adds an enum validator if this is an array of strings or numbers. Equivalent to
  191. * `SchemaString.prototype.enum()` or `SchemaNumber.prototype.enum()`
  192. *
  193. * @param {String|Object} [args...] enumeration values
  194. * @return {SchemaArray} this
  195. */
  196. SchemaArray.prototype.enum = function() {
  197. let arr = this;
  198. while (true) {
  199. const instance = get(arr, 'caster.instance');
  200. if (instance === 'Array') {
  201. arr = arr.caster;
  202. continue;
  203. }
  204. if (instance !== 'String' && instance !== 'Number') {
  205. throw new Error('`enum` can only be set on an array of strings or numbers ' +
  206. ', not ' + instance);
  207. }
  208. break;
  209. }
  210. let enumArray = arguments;
  211. if (!Array.isArray(arguments) && utils.isObject(arguments)) {
  212. enumArray = utils.object.vals(enumArray);
  213. }
  214. arr.caster.enum.apply(arr.caster, enumArray);
  215. return this;
  216. };
  217. /**
  218. * Overrides the getters application for the population special-case
  219. *
  220. * @param {Object} value
  221. * @param {Object} scope
  222. * @api private
  223. */
  224. SchemaArray.prototype.applyGetters = function(value, scope) {
  225. if (scope != null && scope.$__ != null && scope.$populated(this.path)) {
  226. // means the object id was populated
  227. return value;
  228. }
  229. const ret = SchemaType.prototype.applyGetters.call(this, value, scope);
  230. if (Array.isArray(ret)) {
  231. const rawValue = utils.isMongooseArray(ret) ? ret.__array : ret;
  232. const len = rawValue.length;
  233. for (let i = 0; i < len; ++i) {
  234. rawValue[i] = this.caster.applyGetters(rawValue[i], scope);
  235. }
  236. }
  237. return ret;
  238. };
  239. SchemaArray.prototype._applySetters = function(value, scope, init, priorVal) {
  240. if (this.casterConstructor.$isMongooseArray &&
  241. SchemaArray.options.castNonArrays &&
  242. !this[isNestedArraySymbol]) {
  243. // Check nesting levels and wrap in array if necessary
  244. let depth = 0;
  245. let arr = this;
  246. while (arr != null &&
  247. arr.$isMongooseArray &&
  248. !arr.$isMongooseDocumentArray) {
  249. ++depth;
  250. arr = arr.casterConstructor;
  251. }
  252. // No need to wrap empty arrays
  253. if (value != null && value.length !== 0) {
  254. const valueDepth = arrayDepth(value);
  255. if (valueDepth.min === valueDepth.max && valueDepth.max < depth && valueDepth.containsNonArrayItem) {
  256. for (let i = valueDepth.max; i < depth; ++i) {
  257. value = [value];
  258. }
  259. }
  260. }
  261. }
  262. return SchemaType.prototype._applySetters.call(this, value, scope, init, priorVal);
  263. };
  264. /**
  265. * Casts values for set().
  266. *
  267. * @param {Object} value
  268. * @param {Document} doc document that triggers the casting
  269. * @param {Boolean} init whether this is an initialization cast
  270. * @api private
  271. */
  272. SchemaArray.prototype.cast = function(value, doc, init, prev, options) {
  273. // lazy load
  274. MongooseArray || (MongooseArray = require('../types').Array);
  275. let i;
  276. let l;
  277. if (Array.isArray(value)) {
  278. const len = value.length;
  279. if (!len && doc) {
  280. const indexes = doc.schema.indexedPaths();
  281. const arrayPath = this.path;
  282. for (i = 0, l = indexes.length; i < l; ++i) {
  283. const pathIndex = indexes[i][0][arrayPath];
  284. if (pathIndex === '2dsphere' || pathIndex === '2d') {
  285. return;
  286. }
  287. }
  288. // Special case: if this index is on the parent of what looks like
  289. // GeoJSON, skip setting the default to empty array re: #1668, #3233
  290. const arrayGeojsonPath = this.path.endsWith('.coordinates') ?
  291. this.path.substring(0, this.path.lastIndexOf('.')) : null;
  292. if (arrayGeojsonPath != null) {
  293. for (i = 0, l = indexes.length; i < l; ++i) {
  294. const pathIndex = indexes[i][0][arrayGeojsonPath];
  295. if (pathIndex === '2dsphere') {
  296. return;
  297. }
  298. }
  299. }
  300. }
  301. options = options || emptyOpts;
  302. let rawValue = utils.isMongooseArray(value) ? value.__array : value;
  303. value = MongooseArray(rawValue, options.path || this._arrayPath || this.path, doc, this);
  304. rawValue = value.__array;
  305. if (init && doc != null && doc.$__ != null && doc.$populated(this.path)) {
  306. return value;
  307. }
  308. const caster = this.caster;
  309. const isMongooseArray = caster.$isMongooseArray;
  310. if (caster && this.casterConstructor !== Mixed) {
  311. try {
  312. const len = rawValue.length;
  313. for (i = 0; i < len; i++) {
  314. const opts = {};
  315. // Perf: creating `arrayPath` is expensive for large arrays.
  316. // We only need `arrayPath` if this is a nested array, so
  317. // skip if possible.
  318. if (isMongooseArray) {
  319. if (options.arrayPath != null) {
  320. opts.arrayPathIndex = i;
  321. } else if (caster._arrayParentPath != null) {
  322. opts.arrayPathIndex = i;
  323. }
  324. }
  325. rawValue[i] = caster.applySetters(rawValue[i], doc, init, void 0, opts);
  326. }
  327. } catch (e) {
  328. // rethrow
  329. throw new CastError('[' + e.kind + ']', util.inspect(value), this.path + '.' + i, e, this);
  330. }
  331. }
  332. return value;
  333. }
  334. if (init || SchemaArray.options.castNonArrays) {
  335. // gh-2442: if we're loading this from the db and its not an array, mark
  336. // the whole array as modified.
  337. if (!!doc && !!init) {
  338. doc.markModified(this.path);
  339. }
  340. return this.cast([value], doc, init);
  341. }
  342. throw new CastError('Array', util.inspect(value), this.path, null, this);
  343. };
  344. /*!
  345. * ignore
  346. */
  347. SchemaArray.prototype._castForPopulate = function _castForPopulate(value, doc) {
  348. // lazy load
  349. MongooseArray || (MongooseArray = require('../types').Array);
  350. if (Array.isArray(value)) {
  351. let i;
  352. const rawValue = value.__array ? value.__array : value;
  353. const len = rawValue.length;
  354. const caster = this.caster;
  355. if (caster && this.casterConstructor !== Mixed) {
  356. try {
  357. for (i = 0; i < len; i++) {
  358. const opts = {};
  359. // Perf: creating `arrayPath` is expensive for large arrays.
  360. // We only need `arrayPath` if this is a nested array, so
  361. // skip if possible.
  362. if (caster.$isMongooseArray && caster._arrayParentPath != null) {
  363. opts.arrayPathIndex = i;
  364. }
  365. rawValue[i] = caster.cast(rawValue[i], doc, false, void 0, opts);
  366. }
  367. } catch (e) {
  368. // rethrow
  369. throw new CastError('[' + e.kind + ']', util.inspect(value), this.path + '.' + i, e, this);
  370. }
  371. }
  372. return value;
  373. }
  374. throw new CastError('Array', util.inspect(value), this.path, null, this);
  375. };
  376. SchemaArray.prototype.$toObject = SchemaArray.prototype.toObject;
  377. /*!
  378. * Ignore
  379. */
  380. SchemaArray.prototype.discriminator = function(name, schema) {
  381. let arr = this;
  382. while (arr.$isMongooseArray && !arr.$isMongooseDocumentArray) {
  383. arr = arr.casterConstructor;
  384. if (arr == null || typeof arr === 'function') {
  385. throw new MongooseError('You can only add an embedded discriminator on ' +
  386. 'a document array, ' + this.path + ' is a plain array');
  387. }
  388. }
  389. return arr.discriminator(name, schema);
  390. };
  391. /*!
  392. * ignore
  393. */
  394. SchemaArray.prototype.clone = function() {
  395. const options = Object.assign({}, this.options);
  396. const schematype = new this.constructor(this.path, this.caster, options, this.schemaOptions);
  397. schematype.validators = this.validators.slice();
  398. if (this.requiredValidator !== undefined) {
  399. schematype.requiredValidator = this.requiredValidator;
  400. }
  401. return schematype;
  402. };
  403. /**
  404. * Casts values for queries.
  405. *
  406. * @param {String} $conditional
  407. * @param {any} [value]
  408. * @api private
  409. */
  410. SchemaArray.prototype.castForQuery = function($conditional, value) {
  411. let handler;
  412. let val;
  413. if (arguments.length === 2) {
  414. handler = this.$conditionalHandlers[$conditional];
  415. if (!handler) {
  416. throw new Error('Can\'t use ' + $conditional + ' with Array.');
  417. }
  418. val = handler.call(this, value);
  419. } else {
  420. val = $conditional;
  421. let Constructor = this.casterConstructor;
  422. if (val &&
  423. Constructor.discriminators &&
  424. Constructor.schema &&
  425. Constructor.schema.options &&
  426. Constructor.schema.options.discriminatorKey) {
  427. if (typeof val[Constructor.schema.options.discriminatorKey] === 'string' &&
  428. Constructor.discriminators[val[Constructor.schema.options.discriminatorKey]]) {
  429. Constructor = Constructor.discriminators[val[Constructor.schema.options.discriminatorKey]];
  430. } else {
  431. const constructorByValue = getDiscriminatorByValue(Constructor.discriminators, val[Constructor.schema.options.discriminatorKey]);
  432. if (constructorByValue) {
  433. Constructor = constructorByValue;
  434. }
  435. }
  436. }
  437. const proto = this.casterConstructor.prototype;
  438. let method = proto && (proto.castForQuery || proto.cast);
  439. if (!method && Constructor.castForQuery) {
  440. method = Constructor.castForQuery;
  441. }
  442. const caster = this.caster;
  443. if (Array.isArray(val)) {
  444. this.setters.reverse().forEach(setter => {
  445. val = setter.call(this, val, this);
  446. });
  447. val = val.map(function(v) {
  448. if (utils.isObject(v) && v.$elemMatch) {
  449. return v;
  450. }
  451. if (method) {
  452. v = method.call(caster, v);
  453. return v;
  454. }
  455. if (v != null) {
  456. v = new Constructor(v);
  457. return v;
  458. }
  459. return v;
  460. });
  461. } else if (method) {
  462. val = method.call(caster, val);
  463. } else if (val != null) {
  464. val = new Constructor(val);
  465. }
  466. }
  467. return val;
  468. };
  469. function cast$all(val) {
  470. if (!Array.isArray(val)) {
  471. val = [val];
  472. }
  473. val = val.map(function(v) {
  474. if (!utils.isObject(v)) {
  475. return v;
  476. }
  477. if (v.$elemMatch != null) {
  478. return { $elemMatch: cast(this.casterConstructor.schema, v.$elemMatch) };
  479. }
  480. const o = {};
  481. o[this.path] = v;
  482. return cast(this.casterConstructor.schema, o)[this.path];
  483. }, this);
  484. return this.castForQuery(val);
  485. }
  486. function cast$elemMatch(val) {
  487. const keys = Object.keys(val);
  488. const numKeys = keys.length;
  489. for (let i = 0; i < numKeys; ++i) {
  490. const key = keys[i];
  491. const value = val[key];
  492. if (isOperator(key) && value != null) {
  493. val[key] = this.castForQuery(key, value);
  494. }
  495. }
  496. // Is this an embedded discriminator and is the discriminator key set?
  497. // If so, use the discriminator schema. See gh-7449
  498. const discriminatorKey = get(this,
  499. 'casterConstructor.schema.options.discriminatorKey');
  500. const discriminators = get(this, 'casterConstructor.schema.discriminators', {});
  501. if (discriminatorKey != null &&
  502. val[discriminatorKey] != null &&
  503. discriminators[val[discriminatorKey]] != null) {
  504. return cast(discriminators[val[discriminatorKey]], val);
  505. }
  506. return cast(this.casterConstructor.schema, val);
  507. }
  508. const handle = SchemaArray.prototype.$conditionalHandlers = {};
  509. handle.$all = cast$all;
  510. handle.$options = String;
  511. handle.$elemMatch = cast$elemMatch;
  512. handle.$geoIntersects = geospatial.cast$geoIntersects;
  513. handle.$or = createLogicalQueryOperatorHandler('$or');
  514. handle.$and = createLogicalQueryOperatorHandler('$and');
  515. handle.$nor = createLogicalQueryOperatorHandler('$nor');
  516. function createLogicalQueryOperatorHandler(op) {
  517. return function logicalQueryOperatorHandler(val) {
  518. if (!Array.isArray(val)) {
  519. throw new TypeError('conditional ' + op + ' requires an array');
  520. }
  521. const ret = [];
  522. for (const obj of val) {
  523. ret.push(cast(this.casterConstructor.schema, obj));
  524. }
  525. return ret;
  526. };
  527. }
  528. handle.$near =
  529. handle.$nearSphere = geospatial.cast$near;
  530. handle.$within =
  531. handle.$geoWithin = geospatial.cast$within;
  532. handle.$size =
  533. handle.$minDistance =
  534. handle.$maxDistance = castToNumber;
  535. handle.$exists = $exists;
  536. handle.$type = $type;
  537. handle.$eq =
  538. handle.$gt =
  539. handle.$gte =
  540. handle.$lt =
  541. handle.$lte =
  542. handle.$ne =
  543. handle.$regex = SchemaArray.prototype.castForQuery;
  544. // `$in` is special because you can also include an empty array in the query
  545. // like `$in: [1, []]`, see gh-5913
  546. handle.$nin = SchemaType.prototype.$conditionalHandlers.$nin;
  547. handle.$in = SchemaType.prototype.$conditionalHandlers.$in;
  548. /*!
  549. * Module exports.
  550. */
  551. module.exports = SchemaArray;