double.js 5.1 KB

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