array.js 19 KB

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