You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
1.4 KiB

  1. /*!
  2. * Module dependencies.
  3. */
  4. var SchemaType = require('../schematype');
  5. var utils = require('../utils');
  6. /**
  7. * Mixed SchemaType constructor.
  8. *
  9. * @param {String} path
  10. * @param {Object} options
  11. * @inherits SchemaType
  12. * @api public
  13. */
  14. function Mixed(path, options) {
  15. if (options && options.default) {
  16. var def = options.default;
  17. if (Array.isArray(def) && def.length === 0) {
  18. // make sure empty array defaults are handled
  19. options.default = Array;
  20. } else if (!options.shared && utils.isObject(def) && Object.keys(def).length === 0) {
  21. // prevent odd "shared" objects between documents
  22. options.default = function() {
  23. return {};
  24. };
  25. }
  26. }
  27. SchemaType.call(this, path, options, 'Mixed');
  28. }
  29. /**
  30. * This schema type's name, to defend against minifiers that mangle
  31. * function names.
  32. *
  33. * @api public
  34. */
  35. Mixed.schemaName = 'Mixed';
  36. /*!
  37. * Inherits from SchemaType.
  38. */
  39. Mixed.prototype = Object.create(SchemaType.prototype);
  40. Mixed.prototype.constructor = Mixed;
  41. /**
  42. * Casts `val` for Mixed.
  43. *
  44. * _this is a no-op_
  45. *
  46. * @param {Object} value to cast
  47. * @api private
  48. */
  49. Mixed.prototype.cast = function(val) {
  50. return val;
  51. };
  52. /**
  53. * Casts contents for queries.
  54. *
  55. * @param {String} $cond
  56. * @param {any} [val]
  57. * @api private
  58. */
  59. Mixed.prototype.castForQuery = function($cond, val) {
  60. if (arguments.length === 2) {
  61. return val;
  62. }
  63. return $cond;
  64. };
  65. /*!
  66. * Module exports.
  67. */
  68. module.exports = Mixed;