mixed.js 1.6 KB

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