int32.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. 'use strict';
  2. /*!
  3. * Module dependencies.
  4. */
  5. const CastError = require('../error/cast');
  6. const SchemaType = require('../schemaType');
  7. const castInt32 = require('../cast/int32');
  8. const createJSONSchemaTypeDefinition = require('../helpers/createJSONSchemaTypeDefinition');
  9. const handleBitwiseOperator = require('./operators/bitwise');
  10. /**
  11. * Int32 SchemaType constructor.
  12. *
  13. * @param {String} path
  14. * @param {Object} options
  15. * @param {Object} schemaOptions
  16. * @param {Schema} parentSchema
  17. * @inherits SchemaType
  18. * @api public
  19. */
  20. function SchemaInt32(path, options, _schemaOptions, parentSchema) {
  21. SchemaType.call(this, path, options, 'Int32', parentSchema);
  22. }
  23. /**
  24. * This schema type's name, to defend against minifiers that mangle
  25. * function names.
  26. *
  27. * @api public
  28. */
  29. SchemaInt32.schemaName = 'Int32';
  30. SchemaInt32.defaultOptions = {};
  31. /*!
  32. * Inherits from SchemaType.
  33. */
  34. SchemaInt32.prototype = Object.create(SchemaType.prototype);
  35. SchemaInt32.prototype.constructor = SchemaInt32;
  36. /*!
  37. * ignore
  38. */
  39. SchemaInt32._cast = castInt32;
  40. /**
  41. * Sets a default option for all Int32 instances.
  42. *
  43. * #### Example:
  44. *
  45. * // Make all Int32 fields required by default
  46. * mongoose.Schema.Int32.set('required', true);
  47. *
  48. * @param {String} option The option you'd like to set the value for
  49. * @param {Any} value value for option
  50. * @return {undefined}
  51. * @function set
  52. * @static
  53. * @api public
  54. */
  55. SchemaInt32.set = SchemaType.set;
  56. SchemaInt32.setters = [];
  57. /**
  58. * Attaches a getter for all Int32 instances
  59. *
  60. * #### Example:
  61. *
  62. * // Converts int32 to be a represent milliseconds upon access
  63. * mongoose.Schema.Int32.get(v => v == null ? '0 ms' : v.toString() + ' ms');
  64. *
  65. * @param {Function} getter
  66. * @return {this}
  67. * @function get
  68. * @static
  69. * @api public
  70. */
  71. SchemaInt32.get = SchemaType.get;
  72. /*!
  73. * ignore
  74. */
  75. SchemaInt32._defaultCaster = v => {
  76. const INT32_MAX = 0x7FFFFFFF;
  77. const INT32_MIN = -0x80000000;
  78. if (v != null) {
  79. if (typeof v !== 'number' || v !== (v | 0) || v < INT32_MIN || v > INT32_MAX) {
  80. throw new Error();
  81. }
  82. }
  83. return v;
  84. };
  85. /**
  86. * Get/set the function used to cast arbitrary values to 32-bit integers
  87. *
  88. * #### Example:
  89. *
  90. * // Make Mongoose cast NaN to 0
  91. * const defaultCast = mongoose.Schema.Types.Int32.cast();
  92. * mongoose.Schema.Types.Int32.cast(v => {
  93. * if (isNaN(v)) {
  94. * return 0;
  95. * }
  96. * return defaultCast(v);
  97. * });
  98. *
  99. * // Or disable casting for Int32s entirely (only JS numbers within 32-bit integer bounds and null-ish values are permitted)
  100. * mongoose.Schema.Int32.cast(false);
  101. *
  102. *
  103. * @param {Function} caster
  104. * @return {Function}
  105. * @function cast
  106. * @static
  107. * @api public
  108. */
  109. SchemaInt32.cast = function cast(caster) {
  110. if (arguments.length === 0) {
  111. return this._cast;
  112. }
  113. if (caster === false) {
  114. caster = this._defaultCaster;
  115. }
  116. this._cast = caster;
  117. return this._cast;
  118. };
  119. /*!
  120. * ignore
  121. */
  122. SchemaInt32._checkRequired = v => v != null;
  123. /**
  124. * Override the function the required validator uses to check whether a value
  125. * passes the `required` check.
  126. *
  127. * @param {Function} fn
  128. * @return {Function}
  129. * @function checkRequired
  130. * @static
  131. * @api public
  132. */
  133. SchemaInt32.checkRequired = SchemaType.checkRequired;
  134. /**
  135. * Check if the given value satisfies a required validator.
  136. *
  137. * @param {Any} value
  138. * @return {Boolean}
  139. * @api public
  140. */
  141. SchemaInt32.prototype.checkRequired = function(value) {
  142. return this.constructor._checkRequired(value);
  143. };
  144. /**
  145. * Casts to Int32
  146. *
  147. * @param {Object} value
  148. * @param {Object} model this value is optional
  149. * @api private
  150. */
  151. SchemaInt32.prototype.cast = function(value) {
  152. let castInt32;
  153. if (typeof this._castFunction === 'function') {
  154. castInt32 = this._castFunction;
  155. } else if (typeof this.constructor.cast === 'function') {
  156. castInt32 = this.constructor.cast();
  157. } else {
  158. castInt32 = SchemaInt32.cast();
  159. }
  160. try {
  161. return castInt32(value);
  162. } catch (error) {
  163. throw new CastError('Int32', value, this.path, error, this);
  164. }
  165. };
  166. /*!
  167. * ignore
  168. */
  169. const $conditionalHandlers = {
  170. ...SchemaType.prototype.$conditionalHandlers,
  171. $gt: handleSingle,
  172. $gte: handleSingle,
  173. $lt: handleSingle,
  174. $lte: handleSingle,
  175. $bitsAllClear: handleBitwiseOperator,
  176. $bitsAnyClear: handleBitwiseOperator,
  177. $bitsAllSet: handleBitwiseOperator,
  178. $bitsAnySet: handleBitwiseOperator
  179. };
  180. /**
  181. * Contains the handlers for different query operators for this schema type.
  182. * For example, `$conditionalHandlers.$gt` is the function Mongoose calls to cast `$gt` filter operators.
  183. *
  184. * @property $conditionalHandlers
  185. * @memberOf SchemaInt32
  186. * @instance
  187. * @api public
  188. */
  189. Object.defineProperty(SchemaInt32.prototype, '$conditionalHandlers', {
  190. enumerable: false,
  191. value: $conditionalHandlers
  192. });
  193. /*!
  194. * ignore
  195. */
  196. function handleSingle(val, context) {
  197. return this.castForQuery(null, val, context);
  198. }
  199. /**
  200. * Casts contents for queries.
  201. *
  202. * @param {String} $conditional
  203. * @param {any} val
  204. * @api private
  205. */
  206. SchemaInt32.prototype.castForQuery = function($conditional, val, context) {
  207. let handler;
  208. if ($conditional != null) {
  209. handler = this.$conditionalHandlers[$conditional];
  210. if (handler) {
  211. return handler.call(this, val);
  212. }
  213. return this.applySetters(val, context);
  214. }
  215. try {
  216. return this.applySetters(val, context);
  217. } catch (err) {
  218. if (err instanceof CastError && err.path === this.path && this.$fullPath != null) {
  219. err.path = this.$fullPath;
  220. }
  221. throw err;
  222. }
  223. };
  224. /**
  225. * Returns this schema type's representation in a JSON schema.
  226. *
  227. * @param [options]
  228. * @param [options.useBsonType=false] If true, return a representation with `bsonType` for use with MongoDB's `$jsonSchema`.
  229. * @returns {Object} JSON schema properties
  230. */
  231. SchemaInt32.prototype.toJSONSchema = function toJSONSchema(options) {
  232. const isRequired = this.options.required && typeof this.options.required !== 'function';
  233. return createJSONSchemaTypeDefinition('number', 'int', options?.useBsonType, isRequired);
  234. };
  235. SchemaInt32.prototype.autoEncryptionType = function autoEncryptionType() {
  236. return 'int';
  237. };
  238. /*!
  239. * Module exports.
  240. */
  241. module.exports = SchemaInt32;