boolean.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 = this._defaultCaster;
  85. }
  86. this._cast = caster;
  87. return this._cast;
  88. };
  89. /*!
  90. * ignore
  91. */
  92. SchemaBoolean._defaultCaster = v => {
  93. if (v != null && typeof v !== 'boolean') {
  94. throw new Error();
  95. }
  96. return v;
  97. };
  98. /*!
  99. * ignore
  100. */
  101. SchemaBoolean._checkRequired = v => v === true || v === false;
  102. /**
  103. * Override the function the required validator uses to check whether a boolean
  104. * passes the `required` check.
  105. *
  106. * @param {Function} fn
  107. * @return {Function}
  108. * @function checkRequired
  109. * @static
  110. * @api public
  111. */
  112. SchemaBoolean.checkRequired = SchemaType.checkRequired;
  113. /**
  114. * Check if the given value satisfies a required validator. For a boolean
  115. * to satisfy a required validator, it must be strictly equal to true or to
  116. * false.
  117. *
  118. * @param {Any} value
  119. * @return {Boolean}
  120. * @api public
  121. */
  122. SchemaBoolean.prototype.checkRequired = function(value) {
  123. return this.constructor._checkRequired(value);
  124. };
  125. /**
  126. * Configure which values get casted to `true`.
  127. *
  128. * #### Example:
  129. *
  130. * const M = mongoose.model('Test', new Schema({ b: Boolean }));
  131. * new M({ b: 'affirmative' }).b; // undefined
  132. * mongoose.Schema.Boolean.convertToTrue.add('affirmative');
  133. * new M({ b: 'affirmative' }).b; // true
  134. *
  135. * @property convertToTrue
  136. * @type Set
  137. * @api public
  138. */
  139. Object.defineProperty(SchemaBoolean, 'convertToTrue', {
  140. get: () => castBoolean.convertToTrue,
  141. set: v => { castBoolean.convertToTrue = v; }
  142. });
  143. /**
  144. * Configure which values get casted to `false`.
  145. *
  146. * #### Example:
  147. *
  148. * const M = mongoose.model('Test', new Schema({ b: Boolean }));
  149. * new M({ b: 'nay' }).b; // undefined
  150. * mongoose.Schema.Types.Boolean.convertToFalse.add('nay');
  151. * new M({ b: 'nay' }).b; // false
  152. *
  153. * @property convertToFalse
  154. * @type Set
  155. * @api public
  156. */
  157. Object.defineProperty(SchemaBoolean, 'convertToFalse', {
  158. get: () => castBoolean.convertToFalse,
  159. set: v => { castBoolean.convertToFalse = v; }
  160. });
  161. /**
  162. * Casts to boolean
  163. *
  164. * @param {Object} value
  165. * @param {Object} model - this value is optional
  166. * @api private
  167. */
  168. SchemaBoolean.prototype.cast = function(value) {
  169. let castBoolean;
  170. if (typeof this._castFunction === 'function') {
  171. castBoolean = this._castFunction;
  172. } else if (typeof this.constructor.cast === 'function') {
  173. castBoolean = this.constructor.cast();
  174. } else {
  175. castBoolean = SchemaBoolean.cast();
  176. }
  177. try {
  178. return castBoolean(value);
  179. } catch (error) {
  180. throw new CastError('Boolean', value, this.path, error, this);
  181. }
  182. };
  183. SchemaBoolean.$conditionalHandlers =
  184. utils.options(SchemaType.prototype.$conditionalHandlers, {});
  185. /**
  186. * Casts contents for queries.
  187. *
  188. * @param {String} $conditional
  189. * @param {any} val
  190. * @api private
  191. */
  192. SchemaBoolean.prototype.castForQuery = function($conditional, val) {
  193. let handler;
  194. if (arguments.length === 2) {
  195. handler = SchemaBoolean.$conditionalHandlers[$conditional];
  196. if (handler) {
  197. return handler.call(this, val);
  198. }
  199. return this._castForQuery(val);
  200. }
  201. return this._castForQuery($conditional);
  202. };
  203. /**
  204. *
  205. * @api private
  206. */
  207. SchemaBoolean.prototype._castNullish = function _castNullish(v) {
  208. if (typeof v === 'undefined') {
  209. return v;
  210. }
  211. const castBoolean = typeof this.constructor.cast === 'function' ?
  212. this.constructor.cast() :
  213. SchemaBoolean.cast();
  214. if (castBoolean == null) {
  215. return v;
  216. }
  217. if (castBoolean.convertToFalse instanceof Set && castBoolean.convertToFalse.has(v)) {
  218. return false;
  219. }
  220. if (castBoolean.convertToTrue instanceof Set && castBoolean.convertToTrue.has(v)) {
  221. return true;
  222. }
  223. return v;
  224. };
  225. /*!
  226. * Module exports.
  227. */
  228. module.exports = SchemaBoolean;