mixed.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*!
  2. * Module dependencies.
  3. */
  4. 'use strict';
  5. const SchemaType = require('../schemaType');
  6. const symbols = require('./symbols');
  7. const isObject = require('../helpers/isObject');
  8. const utils = require('../utils');
  9. /**
  10. * Mixed SchemaType constructor.
  11. *
  12. * @param {String} path
  13. * @param {Object} options
  14. * @param {Object} _schemaOptions
  15. * @param {Schema} parentSchema
  16. * @inherits SchemaType
  17. * @api public
  18. */
  19. function SchemaMixed(path, options, _schemaOptions, parentSchema) {
  20. if (options?.default) {
  21. const def = options.default;
  22. if (Array.isArray(def) && def.length === 0) {
  23. // make sure empty array defaults are handled
  24. options.default = Array;
  25. } else if (!options.shared && isObject(def) && utils.hasOwnKeys(def) === false) {
  26. // prevent odd "shared" objects between documents
  27. options.default = function() {
  28. return {};
  29. };
  30. }
  31. }
  32. SchemaType.call(this, path, options, 'Mixed', parentSchema);
  33. this[symbols.schemaMixedSymbol] = true;
  34. }
  35. /**
  36. * This schema type's name, to defend against minifiers that mangle
  37. * function names.
  38. *
  39. * @api public
  40. */
  41. SchemaMixed.schemaName = 'Mixed';
  42. SchemaMixed.defaultOptions = {};
  43. /*!
  44. * Inherits from SchemaType.
  45. */
  46. SchemaMixed.prototype = Object.create(SchemaType.prototype);
  47. SchemaMixed.prototype.constructor = SchemaMixed;
  48. /**
  49. * Attaches a getter for all Mixed paths.
  50. *
  51. * #### Example:
  52. *
  53. * // Hide the 'hidden' path
  54. * mongoose.Schema.Mixed.get(v => Object.assign({}, v, { hidden: null }));
  55. *
  56. * const Model = mongoose.model('Test', new Schema({ test: {} }));
  57. * new Model({ test: { hidden: 'Secret!' } }).test.hidden; // null
  58. *
  59. * @param {Function} getter
  60. * @return {this}
  61. * @function get
  62. * @static
  63. * @api public
  64. */
  65. SchemaMixed.get = SchemaType.get;
  66. /**
  67. * Sets a default option for all Mixed instances.
  68. *
  69. * #### Example:
  70. *
  71. * // Make all mixed instances have `required` of true by default.
  72. * mongoose.Schema.Mixed.set('required', true);
  73. *
  74. * const User = mongoose.model('User', new Schema({ test: mongoose.Mixed }));
  75. * new User({ }).validateSync().errors.test.message; // Path `test` is required.
  76. *
  77. * @param {String} option The option you'd like to set the value for
  78. * @param {Any} value value for option
  79. * @return {undefined}
  80. * @function set
  81. * @static
  82. * @api public
  83. */
  84. SchemaMixed.set = SchemaType.set;
  85. SchemaMixed.setters = [];
  86. /**
  87. * Casts `val` for Mixed.
  88. *
  89. * _this is a no-op_
  90. *
  91. * @param {Object} value to cast
  92. * @api private
  93. */
  94. SchemaMixed.prototype.cast = function(val) {
  95. if (val instanceof Error) {
  96. return utils.errorToPOJO(val);
  97. }
  98. return val;
  99. };
  100. /**
  101. * Casts contents for queries.
  102. *
  103. * @param {String} $cond
  104. * @param {any} [val]
  105. * @api private
  106. */
  107. SchemaMixed.prototype.castForQuery = function($cond, val) {
  108. return val;
  109. };
  110. /**
  111. * Returns this schema type's representation in a JSON schema.
  112. *
  113. * @param [options]
  114. * @param [options.useBsonType=false] If true, return a representation with `bsonType` for use with MongoDB's `$jsonSchema`.
  115. * @returns {Object} JSON schema properties
  116. */
  117. // eslint-disable-next-line no-unused-vars
  118. SchemaMixed.prototype.toJSONSchema = function toJSONSchema(_options) {
  119. return {};
  120. };
  121. /*!
  122. * Module exports.
  123. */
  124. module.exports = SchemaMixed;