boolean.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. 'use strict';
  2. /*!
  3. * Module dependencies.
  4. */
  5. const CastError = require('../error/cast');
  6. const SchemaType = require('../schematype');
  7. const castBoolean = require('../cast/boolean');
  8. const utils = require('../utils');
  9. /**
  10. * Boolean SchemaType constructor.
  11. *
  12. * @param {String} path
  13. * @param {Object} options
  14. * @inherits SchemaType
  15. * @api public
  16. */
  17. function SchemaBoolean(path, options) {
  18. SchemaType.call(this, path, options, 'Boolean');
  19. }
  20. /**
  21. * This schema type's name, to defend against minifiers that mangle
  22. * function names.
  23. *
  24. * @api public
  25. */
  26. SchemaBoolean.schemaName = 'Boolean';
  27. SchemaBoolean.defaultOptions = {};
  28. /*!
  29. * Inherits from SchemaType.
  30. */
  31. SchemaBoolean.prototype = Object.create(SchemaType.prototype);
  32. SchemaBoolean.prototype.constructor = SchemaBoolean;
  33. /*!
  34. * ignore
  35. */
  36. SchemaBoolean._cast = castBoolean;
  37. /**
  38. * Sets a default option for all Boolean instances.
  39. *
  40. * ####Example:
  41. *
  42. * // Make all booleans have `default` of false.
  43. * mongoose.Schema.Boolean.set('default', false);
  44. *
  45. * const Order = mongoose.model('Order', new Schema({ isPaid: Boolean }));
  46. * new Order({ }).isPaid; // false
  47. *
  48. * @param {String} option - The option you'd like to set the value for
  49. * @param {*} value - value for option
  50. * @return {undefined}
  51. * @function set
  52. * @static
  53. * @api public
  54. */
  55. SchemaBoolean.set = SchemaType.set;
  56. /**
  57. * Get/set the function used to cast arbitrary values to booleans.
  58. *
  59. * ####Example:
  60. *
  61. * // Make Mongoose cast empty string '' to false.
  62. * const original = mongoose.Schema.Boolean.cast();
  63. * mongoose.Schema.Boolean.cast(v => {
  64. * if (v === '') {
  65. * return false;
  66. * }
  67. * return original(v);
  68. * });
  69. *
  70. * // Or disable casting entirely
  71. * mongoose.Schema.Boolean.cast(false);
  72. *
  73. * @param {Function} caster
  74. * @return {Function}
  75. * @function get
  76. * @static
  77. * @api public
  78. */
  79. SchemaBoolean.cast = function cast(caster) {
  80. if (arguments.length === 0) {
  81. return this._cast;
  82. }
  83. if (caster === false) {
  84. caster = v => {
  85. if (v != null && typeof v !== 'boolean') {
  86. throw new Error();
  87. }
  88. return v;
  89. };
  90. }
  91. this._cast = caster;
  92. return this._cast;
  93. };
  94. /*!
  95. * ignore
  96. */
  97. SchemaBoolean._checkRequired = v => v === true || v === false;
  98. /**
  99. * Override the function the required validator uses to check whether a boolean
  100. * passes the `required` check.
  101. *
  102. * @param {Function} fn
  103. * @return {Function}
  104. * @function checkRequired
  105. * @static
  106. * @api public
  107. */
  108. SchemaBoolean.checkRequired = SchemaType.checkRequired;
  109. /**
  110. * Check if the given value satisfies a required validator. For a boolean
  111. * to satisfy a required validator, it must be strictly equal to true or to
  112. * false.
  113. *
  114. * @param {Any} value
  115. * @return {Boolean}
  116. * @api public
  117. */
  118. SchemaBoolean.prototype.checkRequired = function(value) {
  119. return this.constructor._checkRequired(value);
  120. };
  121. /**
  122. * Configure which values get casted to `true`.
  123. *
  124. * ####Example:
  125. *
  126. * const M = mongoose.model('Test', new Schema({ b: Boolean }));
  127. * new M({ b: 'affirmative' }).b; // undefined
  128. * mongoose.Schema.Boolean.convertToTrue.add('affirmative');
  129. * new M({ b: 'affirmative' }).b; // true
  130. *
  131. * @property convertToTrue
  132. * @type Set
  133. * @api public
  134. */
  135. Object.defineProperty(SchemaBoolean, 'convertToTrue', {
  136. get: () => castBoolean.convertToTrue,
  137. set: v => { castBoolean.convertToTrue = v; }
  138. });
  139. /**
  140. * Configure which values get casted to `false`.
  141. *
  142. * ####Example:
  143. *
  144. * const M = mongoose.model('Test', new Schema({ b: Boolean }));
  145. * new M({ b: 'nay' }).b; // undefined
  146. * mongoose.Schema.Types.Boolean.convertToFalse.add('nay');
  147. * new M({ b: 'nay' }).b; // false
  148. *
  149. * @property convertToFalse
  150. * @type Set
  151. * @api public
  152. */
  153. Object.defineProperty(SchemaBoolean, 'convertToFalse', {
  154. get: () => castBoolean.convertToFalse,
  155. set: v => { castBoolean.convertToFalse = v; }
  156. });
  157. /**
  158. * Casts to boolean
  159. *
  160. * @param {Object} value
  161. * @param {Object} model - this value is optional
  162. * @api private
  163. */
  164. SchemaBoolean.prototype.cast = function(value) {
  165. const castBoolean = typeof this.constructor.cast === 'function' ?
  166. this.constructor.cast() :
  167. SchemaBoolean.cast();
  168. try {
  169. return castBoolean(value);
  170. } catch (error) {
  171. throw new CastError('Boolean', value, this.path, error, this);
  172. }
  173. };
  174. SchemaBoolean.$conditionalHandlers =
  175. utils.options(SchemaType.prototype.$conditionalHandlers, {});
  176. /**
  177. * Casts contents for queries.
  178. *
  179. * @param {String} $conditional
  180. * @param {any} val
  181. * @api private
  182. */
  183. SchemaBoolean.prototype.castForQuery = function($conditional, val) {
  184. let handler;
  185. if (arguments.length === 2) {
  186. handler = SchemaBoolean.$conditionalHandlers[$conditional];
  187. if (handler) {
  188. return handler.call(this, val);
  189. }
  190. return this._castForQuery(val);
  191. }
  192. return this._castForQuery($conditional);
  193. };
  194. /*!
  195. * Module exports.
  196. */
  197. module.exports = SchemaBoolean;