mixed.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. /**
  9. * Mixed SchemaType constructor.
  10. *
  11. * @param {String} path
  12. * @param {Object} options
  13. * @inherits SchemaType
  14. * @api public
  15. */
  16. function Mixed(path, options) {
  17. if (options && options.default) {
  18. const def = options.default;
  19. if (Array.isArray(def) && def.length === 0) {
  20. // make sure empty array defaults are handled
  21. options.default = Array;
  22. } else if (!options.shared && isObject(def) && Object.keys(def).length === 0) {
  23. // prevent odd "shared" objects between documents
  24. options.default = function() {
  25. return {};
  26. };
  27. }
  28. }
  29. SchemaType.call(this, path, options, 'Mixed');
  30. this[symbols.schemaMixedSymbol] = true;
  31. }
  32. /**
  33. * This schema type's name, to defend against minifiers that mangle
  34. * function names.
  35. *
  36. * @api public
  37. */
  38. Mixed.schemaName = 'Mixed';
  39. Mixed.defaultOptions = {};
  40. /*!
  41. * Inherits from SchemaType.
  42. */
  43. Mixed.prototype = Object.create(SchemaType.prototype);
  44. Mixed.prototype.constructor = Mixed;
  45. /**
  46. * Attaches a getter for all Mixed paths.
  47. *
  48. * ####Example:
  49. *
  50. * // Hide the 'hidden' path
  51. * mongoose.Schema.Mixed.get(v => Object.assign({}, v, { hidden: null }));
  52. *
  53. * const Model = mongoose.model('Test', new Schema({ test: {} }));
  54. * new Model({ test: { hidden: 'Secret!' } }).test.hidden; // null
  55. *
  56. * @param {Function} getter
  57. * @return {this}
  58. * @function get
  59. * @static
  60. * @api public
  61. */
  62. Mixed.get = SchemaType.get;
  63. /**
  64. * Sets a default option for all Mixed instances.
  65. *
  66. * ####Example:
  67. *
  68. * // Make all mixed instances have `required` of true by default.
  69. * mongoose.Schema.Mixed.set('required', true);
  70. *
  71. * const User = mongoose.model('User', new Schema({ test: mongoose.Mixed }));
  72. * new User({ }).validateSync().errors.test.message; // Path `test` is required.
  73. *
  74. * @param {String} option - The option you'd like to set the value for
  75. * @param {*} value - value for option
  76. * @return {undefined}
  77. * @function set
  78. * @static
  79. * @api public
  80. */
  81. Mixed.set = SchemaType.set;
  82. /**
  83. * Casts `val` for Mixed.
  84. *
  85. * _this is a no-op_
  86. *
  87. * @param {Object} value to cast
  88. * @api private
  89. */
  90. Mixed.prototype.cast = function(val) {
  91. return val;
  92. };
  93. /**
  94. * Casts contents for queries.
  95. *
  96. * @param {String} $cond
  97. * @param {any} [val]
  98. * @api private
  99. */
  100. Mixed.prototype.castForQuery = function($cond, val) {
  101. if (arguments.length === 2) {
  102. return val;
  103. }
  104. return $cond;
  105. };
  106. /*!
  107. * Module exports.
  108. */
  109. module.exports = Mixed;