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.

83 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 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);
  30. };
  31. /*!
  32. * Inherits from SchemaType.
  33. */
  34. Mixed.prototype.__proto__ = SchemaType.prototype;
  35. /**
  36. * Required validator
  37. *
  38. * @api private
  39. */
  40. Mixed.prototype.checkRequired = function (val) {
  41. return true;
  42. };
  43. /**
  44. * Casts `val` for Mixed.
  45. *
  46. * _this is a no-op_
  47. *
  48. * @param {Object} value to cast
  49. * @api private
  50. */
  51. Mixed.prototype.cast = function (val) {
  52. return val;
  53. };
  54. /**
  55. * Casts contents for queries.
  56. *
  57. * @param {String} $cond
  58. * @param {any} [val]
  59. * @api private
  60. */
  61. Mixed.prototype.castForQuery = function ($cond, val) {
  62. if (arguments.length === 2) return val;
  63. return $cond;
  64. };
  65. /*!
  66. * Module exports.
  67. */
  68. module.exports = Mixed;