Ohm-Management - Projektarbeit B-ME
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.

boolean.js 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. /*!
  28. * Inherits from SchemaType.
  29. */
  30. SchemaBoolean.prototype = Object.create(SchemaType.prototype);
  31. SchemaBoolean.prototype.constructor = SchemaBoolean;
  32. /*!
  33. * ignore
  34. */
  35. SchemaBoolean._cast = castBoolean;
  36. /**
  37. * Get/set the function used to cast arbitrary values to booleans.
  38. *
  39. * ####Example:
  40. *
  41. * // Make Mongoose cast empty string '' to false.
  42. * const original = mongoose.Schema.Boolean.cast();
  43. * mongoose.Schema.Boolean.cast(v => {
  44. * if (v === '') {
  45. * return false;
  46. * }
  47. * return original(v);
  48. * });
  49. *
  50. * // Or disable casting entirely
  51. * mongoose.Schema.Boolean.cast(false);
  52. *
  53. * @param {Function} caster
  54. * @return {Function}
  55. * @function get
  56. * @static
  57. * @api public
  58. */
  59. SchemaBoolean.cast = function cast(caster) {
  60. if (arguments.length === 0) {
  61. return this._cast;
  62. }
  63. if (caster === false) {
  64. caster = v => {
  65. if (v != null && typeof v !== 'boolean') {
  66. throw new Error();
  67. }
  68. return v;
  69. };
  70. }
  71. this._cast = caster;
  72. return this._cast;
  73. };
  74. /*!
  75. * ignore
  76. */
  77. SchemaBoolean._checkRequired = v => v === true || v === false;
  78. /**
  79. * Override the function the required validator uses to check whether a boolean
  80. * passes the `required` check.
  81. *
  82. * @param {Function} fn
  83. * @return {Function}
  84. * @function checkRequired
  85. * @static
  86. * @api public
  87. */
  88. SchemaBoolean.checkRequired = SchemaType.checkRequired;
  89. /**
  90. * Check if the given value satisfies a required validator. For a boolean
  91. * to satisfy a required validator, it must be strictly equal to true or to
  92. * false.
  93. *
  94. * @param {Any} value
  95. * @return {Boolean}
  96. * @api public
  97. */
  98. SchemaBoolean.prototype.checkRequired = function(value) {
  99. return this.constructor._checkRequired(value);
  100. };
  101. /**
  102. * Configure which values get casted to `true`.
  103. *
  104. * ####Example:
  105. *
  106. * const M = mongoose.model('Test', new Schema({ b: Boolean }));
  107. * new M({ b: 'affirmative' }).b; // undefined
  108. * mongoose.Schema.Boolean.convertToTrue.add('affirmative');
  109. * new M({ b: 'affirmative' }).b; // true
  110. *
  111. * @property convertToTrue
  112. * @type Set
  113. * @api public
  114. */
  115. Object.defineProperty(SchemaBoolean, 'convertToTrue', {
  116. get: () => castBoolean.convertToTrue,
  117. set: v => { castBoolean.convertToTrue = v; }
  118. });
  119. /**
  120. * Configure which values get casted to `false`.
  121. *
  122. * ####Example:
  123. *
  124. * const M = mongoose.model('Test', new Schema({ b: Boolean }));
  125. * new M({ b: 'nay' }).b; // undefined
  126. * mongoose.Schema.Types.Boolean.convertToFalse.add('nay');
  127. * new M({ b: 'nay' }).b; // false
  128. *
  129. * @property convertToFalse
  130. * @type Set
  131. * @api public
  132. */
  133. Object.defineProperty(SchemaBoolean, 'convertToFalse', {
  134. get: () => castBoolean.convertToFalse,
  135. set: v => { castBoolean.convertToFalse = v; }
  136. });
  137. /**
  138. * Casts to boolean
  139. *
  140. * @param {Object} value
  141. * @param {Object} model - this value is optional
  142. * @api private
  143. */
  144. SchemaBoolean.prototype.cast = function(value) {
  145. try {
  146. return this.constructor.cast()(value);
  147. } catch (error) {
  148. throw new CastError('Boolean', value, this.path);
  149. }
  150. };
  151. SchemaBoolean.$conditionalHandlers =
  152. utils.options(SchemaType.prototype.$conditionalHandlers, {});
  153. /**
  154. * Casts contents for queries.
  155. *
  156. * @param {String} $conditional
  157. * @param {any} val
  158. * @api private
  159. */
  160. SchemaBoolean.prototype.castForQuery = function($conditional, val) {
  161. let handler;
  162. if (arguments.length === 2) {
  163. handler = SchemaBoolean.$conditionalHandlers[$conditional];
  164. if (handler) {
  165. return handler.call(this, val);
  166. }
  167. return this._castForQuery(val);
  168. }
  169. return this._castForQuery($conditional);
  170. };
  171. /*!
  172. * Module exports.
  173. */
  174. module.exports = SchemaBoolean;