decimal128.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 utils = require('../utils');
  9. const isBsonType = require('../helpers/isBsonType');
  10. /**
  11. * Decimal128 SchemaType constructor.
  12. *
  13. * @param {String} key
  14. * @param {Object} options
  15. * @inherits SchemaType
  16. * @api public
  17. */
  18. function Decimal128(key, options) {
  19. SchemaType.call(this, key, options, 'Decimal128');
  20. }
  21. /**
  22. * This schema type's name, to defend against minifiers that mangle
  23. * function names.
  24. *
  25. * @api public
  26. */
  27. Decimal128.schemaName = 'Decimal128';
  28. Decimal128.defaultOptions = {};
  29. /*!
  30. * Inherits from SchemaType.
  31. */
  32. Decimal128.prototype = Object.create(SchemaType.prototype);
  33. Decimal128.prototype.constructor = Decimal128;
  34. /*!
  35. * ignore
  36. */
  37. Decimal128._cast = castDecimal128;
  38. /**
  39. * Sets a default option for all Decimal128 instances.
  40. *
  41. * #### Example:
  42. *
  43. * // Make all decimal 128s have `required` of true by default.
  44. * mongoose.Schema.Decimal128.set('required', true);
  45. *
  46. * const User = mongoose.model('User', new Schema({ test: mongoose.Decimal128 }));
  47. * new User({ }).validateSync().errors.test.message; // Path `test` is required.
  48. *
  49. * @param {String} option - The option you'd like to set the value for
  50. * @param {*} value - value for option
  51. * @return {undefined}
  52. * @function set
  53. * @static
  54. * @api public
  55. */
  56. Decimal128.set = SchemaType.set;
  57. /**
  58. * Get/set the function used to cast arbitrary values to decimals.
  59. *
  60. * #### Example:
  61. *
  62. * // Make Mongoose only refuse to cast numbers as decimal128
  63. * const original = mongoose.Schema.Types.Decimal128.cast();
  64. * mongoose.Decimal128.cast(v => {
  65. * assert.ok(typeof v !== 'number');
  66. * return original(v);
  67. * });
  68. *
  69. * // Or disable casting entirely
  70. * mongoose.Decimal128.cast(false);
  71. *
  72. * @param {Function} [caster]
  73. * @return {Function}
  74. * @function get
  75. * @static
  76. * @api public
  77. */
  78. Decimal128.cast = function cast(caster) {
  79. if (arguments.length === 0) {
  80. return this._cast;
  81. }
  82. if (caster === false) {
  83. caster = this._defaultCaster;
  84. }
  85. this._cast = caster;
  86. return this._cast;
  87. };
  88. /*!
  89. * ignore
  90. */
  91. Decimal128._defaultCaster = v => {
  92. if (v != null && !isBsonType(v, 'Decimal128')) {
  93. throw new Error();
  94. }
  95. return v;
  96. };
  97. /*!
  98. * ignore
  99. */
  100. Decimal128._checkRequired = v => isBsonType(v, 'Decimal128');
  101. /**
  102. * Override the function the required validator uses to check whether a string
  103. * passes the `required` check.
  104. *
  105. * @param {Function} fn
  106. * @return {Function}
  107. * @function checkRequired
  108. * @static
  109. * @api public
  110. */
  111. Decimal128.checkRequired = SchemaType.checkRequired;
  112. /**
  113. * Check if the given value satisfies a required validator.
  114. *
  115. * @param {Any} value
  116. * @param {Document} doc
  117. * @return {Boolean}
  118. * @api public
  119. */
  120. Decimal128.prototype.checkRequired = function checkRequired(value, doc) {
  121. if (SchemaType._isRef(this, value, doc, true)) {
  122. return !!value;
  123. }
  124. // `require('util').inherits()` does **not** copy static properties, and
  125. // plugins like mongoose-float use `inherits()` for pre-ES6.
  126. const _checkRequired = typeof this.constructor.checkRequired === 'function' ?
  127. this.constructor.checkRequired() :
  128. Decimal128.checkRequired();
  129. return _checkRequired(value);
  130. };
  131. /**
  132. * Casts to Decimal128
  133. *
  134. * @param {Object} value
  135. * @param {Object} doc
  136. * @param {Boolean} init whether this is an initialization cast
  137. * @api private
  138. */
  139. Decimal128.prototype.cast = function(value, doc, init) {
  140. if (SchemaType._isRef(this, value, doc, init)) {
  141. if (isBsonType(value, 'Decimal128')) {
  142. return value;
  143. }
  144. return this._castRef(value, doc, init);
  145. }
  146. let castDecimal128;
  147. if (typeof this._castFunction === 'function') {
  148. castDecimal128 = this._castFunction;
  149. } else if (typeof this.constructor.cast === 'function') {
  150. castDecimal128 = this.constructor.cast();
  151. } else {
  152. castDecimal128 = Decimal128.cast();
  153. }
  154. try {
  155. return castDecimal128(value);
  156. } catch (error) {
  157. throw new CastError('Decimal128', value, this.path, error, this);
  158. }
  159. };
  160. /*!
  161. * ignore
  162. */
  163. function handleSingle(val) {
  164. return this.cast(val);
  165. }
  166. Decimal128.prototype.$conditionalHandlers =
  167. utils.options(SchemaType.prototype.$conditionalHandlers, {
  168. $gt: handleSingle,
  169. $gte: handleSingle,
  170. $lt: handleSingle,
  171. $lte: handleSingle
  172. });
  173. /*!
  174. * Module exports.
  175. */
  176. module.exports = Decimal128;