decimal128.js 6.1 KB

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