array.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  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. let arr = [];
  94. if (fn) {
  95. arr = defaultArr.call(this);
  96. } else if (defaultArr != null) {
  97. arr = arr.concat(defaultArr);
  98. }
  99. // Leave it up to `cast()` to convert the array
  100. return arr;
  101. };
  102. defaultFn.$runBeforeSetters = !fn;
  103. this.default(defaultFn);
  104. }
  105. }
  106. /**
  107. * This schema type's name, to defend against minifiers that mangle
  108. * function names.
  109. *
  110. * @api public
  111. */
  112. SchemaArray.schemaName = 'Array';
  113. /**
  114. * Options for all arrays.
  115. *
  116. * - `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.
  117. *
  118. * @static options
  119. * @api public
  120. */
  121. SchemaArray.options = { castNonArrays: true };
  122. SchemaArray.defaultOptions = {};
  123. /**
  124. * Sets a default option for all Array instances.
  125. *
  126. * ####Example:
  127. *
  128. * // Make all Array instances have `required` of true by default.
  129. * mongoose.Schema.Array.set('required', true);
  130. *
  131. * const User = mongoose.model('User', new Schema({ test: Array }));
  132. * new User({ }).validateSync().errors.test.message; // Path `test` is required.
  133. *
  134. * @param {String} option - The option you'd like to set the value for
  135. * @param {*} value - value for option
  136. * @return {undefined}
  137. * @function set
  138. * @static
  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. * @static
  168. * @api public
  169. */
  170. SchemaArray.checkRequired = SchemaType.checkRequired;
  171. /**
  172. * Check if the given value satisfies the `required` validator.
  173. *
  174. * @param {Any} value
  175. * @param {Document} doc
  176. * @return {Boolean}
  177. * @api public
  178. */
  179. SchemaArray.prototype.checkRequired = function checkRequired(value, doc) {
  180. if (SchemaType._isRef(this, value, doc, true)) {
  181. return !!value;
  182. }
  183. // `require('util').inherits()` does **not** copy static properties, and
  184. // plugins like mongoose-float use `inherits()` for pre-ES6.
  185. const _checkRequired = typeof this.constructor.checkRequired == 'function' ?
  186. this.constructor.checkRequired() :
  187. SchemaArray.checkRequired();
  188. return _checkRequired(value);
  189. };
  190. /**
  191. * Adds an enum validator if this is an array of strings or numbers. Equivalent to
  192. * `SchemaString.prototype.enum()` or `SchemaNumber.prototype.enum()`
  193. *
  194. * @param {String|Object} [args...] enumeration values
  195. * @return {SchemaArray} this
  196. */
  197. SchemaArray.prototype.enum = function() {
  198. let arr = this;
  199. while (true) {
  200. const instance = get(arr, 'caster.instance');
  201. if (instance === 'Array') {
  202. arr = arr.caster;
  203. continue;
  204. }
  205. if (instance !== 'String' && instance !== 'Number') {
  206. throw new Error('`enum` can only be set on an array of strings or numbers ' +
  207. ', not ' + instance);
  208. }
  209. break;
  210. }
  211. let enumArray = arguments;
  212. if (!Array.isArray(arguments) && utils.isObject(arguments)) {
  213. enumArray = utils.object.vals(enumArray);
  214. }
  215. arr.caster.enum.apply(arr.caster, enumArray);
  216. return this;
  217. };
  218. /**
  219. * Overrides the getters application for the population special-case
  220. *
  221. * @param {Object} value
  222. * @param {Object} scope
  223. * @api private
  224. */
  225. SchemaArray.prototype.applyGetters = function(value, scope) {
  226. if (scope != null && scope.$__ != null && scope.populated(this.path)) {
  227. // means the object id was populated
  228. return value;
  229. }
  230. const ret = SchemaType.prototype.applyGetters.call(this, value, scope);
  231. if (Array.isArray(ret)) {
  232. const len = ret.length;
  233. for (let i = 0; i < len; ++i) {
  234. ret[i] = this.caster.applyGetters(ret[i], scope);
  235. }
  236. }
  237. return ret;
  238. };
  239. SchemaArray.prototype._applySetters = function(value, scope, init, priorVal) {
  240. if (this.casterConstructor instanceof SchemaArray &&
  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 instanceof SchemaArray &&
  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.substr(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. value = MongooseArray(value, options.path || this._arrayPath || this.path, doc, this);
  303. if (init && doc != null && doc.$__ != null && doc.populated(this.path)) {
  304. return value;
  305. }
  306. const caster = this.caster;
  307. if (caster && this.casterConstructor !== Mixed) {
  308. try {
  309. for (i = 0; i < len; i++) {
  310. // Special case: number arrays disallow undefined.
  311. // Re: gh-840
  312. // See commit 1298fe92d2c790a90594bd08199e45a4a09162a6
  313. if (caster.instance === 'Number' && value[i] === void 0) {
  314. throw new MongooseError('Mongoose number arrays disallow storing undefined');
  315. }
  316. const opts = {};
  317. // Perf: creating `arrayPath` is expensive for large arrays.
  318. // We only need `arrayPath` if this is a nested array, so
  319. // skip if possible.
  320. if (caster.$isMongooseArray) {
  321. if (options.arrayPath != null) {
  322. opts.arrayPathIndex = i;
  323. } else if (caster._arrayParentPath != null) {
  324. opts.arrayPathIndex = i;
  325. }
  326. }
  327. value[i] = caster.applySetters(value[i], doc, init, void 0, opts);
  328. }
  329. } catch (e) {
  330. // rethrow
  331. throw new CastError('[' + e.kind + ']', util.inspect(value), this.path + '.' + i, e, this);
  332. }
  333. }
  334. return value;
  335. }
  336. if (init || SchemaArray.options.castNonArrays) {
  337. // gh-2442: if we're loading this from the db and its not an array, mark
  338. // the whole array as modified.
  339. if (!!doc && !!init) {
  340. doc.markModified(this.path);
  341. }
  342. return this.cast([value], doc, init);
  343. }
  344. throw new CastError('Array', util.inspect(value), this.path, null, this);
  345. };
  346. /*!
  347. * ignore
  348. */
  349. SchemaArray.prototype._castForPopulate = function _castForPopulate(value, doc) {
  350. // lazy load
  351. MongooseArray || (MongooseArray = require('../types').Array);
  352. if (Array.isArray(value)) {
  353. let i;
  354. const len = value.length;
  355. const caster = this.caster;
  356. if (caster && this.casterConstructor !== Mixed) {
  357. try {
  358. for (i = 0; i < len; i++) {
  359. const opts = {};
  360. // Perf: creating `arrayPath` is expensive for large arrays.
  361. // We only need `arrayPath` if this is a nested array, so
  362. // skip if possible.
  363. if (caster.$isMongooseArray && caster._arrayParentPath != null) {
  364. opts.arrayPathIndex = i;
  365. }
  366. value[i] = caster.cast(value[i], doc, false, void 0, opts);
  367. }
  368. } catch (e) {
  369. // rethrow
  370. throw new CastError('[' + e.kind + ']', util.inspect(value), this.path + '.' + i, e, this);
  371. }
  372. }
  373. return value;
  374. }
  375. throw new CastError('Array', util.inspect(value), this.path, null, this);
  376. };
  377. /*!
  378. * Ignore
  379. */
  380. SchemaArray.prototype.discriminator = function(name, schema) {
  381. let arr = this; // eslint-disable-line consistent-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. const o = {};
  476. o[this.path] = v;
  477. return cast(this.casterConstructor.schema, o)[this.path];
  478. }
  479. return v;
  480. }, this);
  481. return this.castForQuery(val);
  482. }
  483. function cast$elemMatch(val) {
  484. const keys = Object.keys(val);
  485. const numKeys = keys.length;
  486. for (let i = 0; i < numKeys; ++i) {
  487. const key = keys[i];
  488. const value = val[key];
  489. if (isOperator(key) && value != null) {
  490. val[key] = this.castForQuery(key, value);
  491. }
  492. }
  493. // Is this an embedded discriminator and is the discriminator key set?
  494. // If so, use the discriminator schema. See gh-7449
  495. const discriminatorKey = get(this,
  496. 'casterConstructor.schema.options.discriminatorKey');
  497. const discriminators = get(this, 'casterConstructor.schema.discriminators', {});
  498. if (discriminatorKey != null &&
  499. val[discriminatorKey] != null &&
  500. discriminators[val[discriminatorKey]] != null) {
  501. return cast(discriminators[val[discriminatorKey]], val);
  502. }
  503. return cast(this.casterConstructor.schema, val);
  504. }
  505. const handle = SchemaArray.prototype.$conditionalHandlers = {};
  506. handle.$all = cast$all;
  507. handle.$options = String;
  508. handle.$elemMatch = cast$elemMatch;
  509. handle.$geoIntersects = geospatial.cast$geoIntersects;
  510. handle.$or = createLogicalQueryOperatorHandler('$or');
  511. handle.$and = createLogicalQueryOperatorHandler('$and');
  512. handle.$nor = createLogicalQueryOperatorHandler('$nor');
  513. function createLogicalQueryOperatorHandler(op) {
  514. return function logicalQueryOperatorHandler(val) {
  515. if (!Array.isArray(val)) {
  516. throw new TypeError('conditional ' + op + ' requires an array');
  517. }
  518. const ret = [];
  519. for (const obj of val) {
  520. ret.push(cast(this.casterConstructor.schema, obj));
  521. }
  522. return ret;
  523. };
  524. }
  525. handle.$near =
  526. handle.$nearSphere = geospatial.cast$near;
  527. handle.$within =
  528. handle.$geoWithin = geospatial.cast$within;
  529. handle.$size =
  530. handle.$minDistance =
  531. handle.$maxDistance = castToNumber;
  532. handle.$exists = $exists;
  533. handle.$type = $type;
  534. handle.$eq =
  535. handle.$gt =
  536. handle.$gte =
  537. handle.$lt =
  538. handle.$lte =
  539. handle.$ne =
  540. handle.$regex = SchemaArray.prototype.castForQuery;
  541. // `$in` is special because you can also include an empty array in the query
  542. // like `$in: [1, []]`, see gh-5913
  543. handle.$nin = SchemaType.prototype.$conditionalHandlers.$nin;
  544. handle.$in = SchemaType.prototype.$conditionalHandlers.$in;
  545. /*!
  546. * Module exports.
  547. */
  548. module.exports = SchemaArray;