documentArrayElement.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*!
  2. * Module dependencies.
  3. */
  4. 'use strict';
  5. const MongooseError = require('../error/mongooseError');
  6. const SchemaType = require('../schemaType');
  7. const SchemaSubdocument = require('./subdocument');
  8. const getConstructor = require('../helpers/discriminator/getConstructor');
  9. /**
  10. * DocumentArrayElement SchemaType constructor. Mongoose calls this internally when you define a new document array in your schema.
  11. *
  12. * #### Example:
  13. * const schema = new Schema({ users: [{ name: String }] });
  14. * schema.path('users.$'); // SchemaDocumentArrayElement with schema `new Schema({ name: String })`
  15. *
  16. * @param {String} path
  17. * @param {Schema} schema
  18. * @param {Object} options
  19. * @param {Object} options
  20. * @param {Schema} parentSchema
  21. * @inherits SchemaType
  22. * @api public
  23. */
  24. function SchemaDocumentArrayElement(path, schema, options, parentSchema) {
  25. this.$parentSchemaType = options?.$parentSchemaType;
  26. if (!this.$parentSchemaType) {
  27. throw new MongooseError('Cannot create DocumentArrayElement schematype without a parent');
  28. }
  29. delete options.$parentSchemaType;
  30. SchemaType.call(this, path, options, 'DocumentArrayElement', parentSchema);
  31. this.$isMongooseDocumentArrayElement = true;
  32. this.Constructor = options?.Constructor;
  33. this.schema = schema;
  34. }
  35. /**
  36. * This schema type's name, to defend against minifiers that mangle
  37. * function names.
  38. *
  39. * @api public
  40. */
  41. SchemaDocumentArrayElement.schemaName = 'DocumentArrayElement';
  42. SchemaDocumentArrayElement.defaultOptions = {};
  43. /*!
  44. * Inherits from SchemaType.
  45. */
  46. SchemaDocumentArrayElement.prototype = Object.create(SchemaType.prototype);
  47. SchemaDocumentArrayElement.prototype.constructor = SchemaDocumentArrayElement;
  48. /**
  49. * Casts `val` for DocumentArrayElement.
  50. *
  51. * @param {Object} value to cast
  52. * @api private
  53. */
  54. SchemaDocumentArrayElement.prototype.cast = function(...args) {
  55. return this.$parentSchemaType.cast(...args)[0];
  56. };
  57. /**
  58. * Async validation on this individual array element
  59. *
  60. * @api public
  61. */
  62. SchemaDocumentArrayElement.prototype.doValidate = async function doValidate(value, scope, options) {
  63. const Constructor = getConstructor(this.Constructor, value);
  64. if (value && !(value instanceof Constructor)) {
  65. value = new Constructor(value, scope, null, null, options?.index ?? null);
  66. }
  67. return SchemaSubdocument.prototype.doValidate.call(this, value, scope, options);
  68. };
  69. /**
  70. * Clone the current SchemaType
  71. *
  72. * @return {DocumentArrayElement} The cloned instance
  73. * @api private
  74. */
  75. SchemaDocumentArrayElement.prototype.clone = function() {
  76. this.options.$parentSchemaType = this.$parentSchemaType;
  77. const ret = SchemaType.prototype.clone.apply(this, arguments);
  78. delete this.options.$parentSchemaType;
  79. ret.Constructor = this.Constructor;
  80. ret.schema = this.schema;
  81. return ret;
  82. };
  83. /*!
  84. * Module exports.
  85. */
  86. module.exports = SchemaDocumentArrayElement;