schemaArrayOptions.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. 'use strict';
  2. const SchemaTypeOptions = require('./schemaTypeOptions');
  3. /**
  4. * The options defined on an Array schematype.
  5. *
  6. * #### Example:
  7. *
  8. * const schema = new Schema({ tags: [String] });
  9. * schema.path('tags').options; // SchemaArrayOptions instance
  10. *
  11. * @api public
  12. * @inherits SchemaTypeOptions
  13. * @constructor SchemaArrayOptions
  14. */
  15. class SchemaArrayOptions extends SchemaTypeOptions {}
  16. const opts = require('./propertyOptions');
  17. /**
  18. * If this is an array of strings, an array of allowed values for this path.
  19. * Throws an error if this array isn't an array of strings.
  20. *
  21. * @api public
  22. * @property enum
  23. * @memberOf SchemaArrayOptions
  24. * @type {Array}
  25. * @instance
  26. */
  27. Object.defineProperty(SchemaArrayOptions.prototype, 'enum', opts);
  28. /**
  29. * If set, specifies the type of this array's values. Equivalent to setting
  30. * `type` to an array whose first element is `of`.
  31. *
  32. * #### Example:
  33. *
  34. * // `arr` is an array of numbers.
  35. * new Schema({ arr: [Number] });
  36. * // Equivalent way to define `arr` as an array of numbers
  37. * new Schema({ arr: { type: Array, of: Number } });
  38. *
  39. * @api public
  40. * @property of
  41. * @memberOf SchemaArrayOptions
  42. * @type {Function|String}
  43. * @instance
  44. */
  45. Object.defineProperty(SchemaArrayOptions.prototype, 'of', opts);
  46. /**
  47. * If set to `false`, will always deactivate casting non-array values to arrays.
  48. * If set to `true`, will cast non-array values to arrays if `init` and `SchemaArray.options.castNonArrays` are also `true`
  49. *
  50. * #### Example:
  51. *
  52. * const Model = db.model('Test', new Schema({ x1: { castNonArrays: false, type: [String] } }));
  53. * const doc = new Model({ x1: "some non-array value" });
  54. * await doc.validate(); // Errors with "CastError"
  55. *
  56. * @api public
  57. * @property castNonArrays
  58. * @memberOf SchemaArrayOptions
  59. * @type {Boolean}
  60. * @instance
  61. */
  62. Object.defineProperty(SchemaArrayOptions.prototype, 'castNonArrays', opts);
  63. /*!
  64. * ignore
  65. */
  66. module.exports = SchemaArrayOptions;