mixed.js 2.7 KB

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