123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438 |
- 'use strict';
- const MongooseError = require('../error/index');
- const SchemaNumberOptions = require('../options/SchemaNumberOptions');
- const SchemaType = require('../schematype');
- const castNumber = require('../cast/number');
- const handleBitwiseOperator = require('./operators/bitwise');
- const utils = require('../utils');
- const CastError = SchemaType.CastError;
- function SchemaNumber(key, options) {
- SchemaType.call(this, key, options, 'Number');
- }
- SchemaNumber.get = SchemaType.get;
- SchemaNumber.set = SchemaType.set;
- SchemaNumber._cast = castNumber;
- SchemaNumber.cast = function cast(caster) {
- if (arguments.length === 0) {
- return this._cast;
- }
- if (caster === false) {
- caster = this._defaultCaster;
- }
- this._cast = caster;
- return this._cast;
- };
- SchemaNumber._defaultCaster = v => {
- if (typeof v !== 'number') {
- throw new Error();
- }
- return v;
- };
- SchemaNumber.schemaName = 'Number';
- SchemaNumber.defaultOptions = {};
- SchemaNumber.prototype = Object.create(SchemaType.prototype);
- SchemaNumber.prototype.constructor = SchemaNumber;
- SchemaNumber.prototype.OptionsConstructor = SchemaNumberOptions;
- SchemaNumber._checkRequired = v => typeof v === 'number' || v instanceof Number;
- SchemaNumber.checkRequired = SchemaType.checkRequired;
- SchemaNumber.prototype.checkRequired = function checkRequired(value, doc) {
- if (typeof value === 'object' && SchemaType._isRef(this, value, doc, true)) {
- return value != null;
- }
-
-
- const _checkRequired = typeof this.constructor.checkRequired === 'function' ?
- this.constructor.checkRequired() :
- SchemaNumber.checkRequired();
- return _checkRequired(value);
- };
- SchemaNumber.prototype.min = function(value, message) {
- if (this.minValidator) {
- this.validators = this.validators.filter(function(v) {
- return v.validator !== this.minValidator;
- }, this);
- }
- if (value !== null && value !== undefined) {
- let msg = message || MongooseError.messages.Number.min;
- msg = msg.replace(/{MIN}/, value);
- this.validators.push({
- validator: this.minValidator = function(v) {
- return v == null || v >= value;
- },
- message: msg,
- type: 'min',
- min: value
- });
- }
- return this;
- };
- SchemaNumber.prototype.max = function(value, message) {
- if (this.maxValidator) {
- this.validators = this.validators.filter(function(v) {
- return v.validator !== this.maxValidator;
- }, this);
- }
- if (value !== null && value !== undefined) {
- let msg = message || MongooseError.messages.Number.max;
- msg = msg.replace(/{MAX}/, value);
- this.validators.push({
- validator: this.maxValidator = function(v) {
- return v == null || v <= value;
- },
- message: msg,
- type: 'max',
- max: value
- });
- }
- return this;
- };
- SchemaNumber.prototype.enum = function(values, message) {
- if (this.enumValidator) {
- this.validators = this.validators.filter(function(v) {
- return v.validator !== this.enumValidator;
- }, this);
- }
- if (!Array.isArray(values)) {
- const isObjectSyntax = utils.isPOJO(values) && values.values != null;
- if (isObjectSyntax) {
- message = values.message;
- values = values.values;
- } else if (typeof values === 'number') {
- values = Array.prototype.slice.call(arguments);
- message = null;
- }
- if (utils.isPOJO(values)) {
- values = Object.values(values);
- }
- message = message || MongooseError.messages.Number.enum;
- }
- message = message == null ? MongooseError.messages.Number.enum : message;
- this.enumValidator = v => v == null || values.indexOf(v) !== -1;
- this.validators.push({
- validator: this.enumValidator,
- message: message,
- type: 'enum',
- enumValues: values
- });
- return this;
- };
- SchemaNumber.prototype.cast = function(value, doc, init) {
- if (typeof value !== 'number' && SchemaType._isRef(this, value, doc, init)) {
- if (value == null || utils.isNonBuiltinObject(value)) {
- return this._castRef(value, doc, init);
- }
- }
- const val = value && typeof value._id !== 'undefined' ?
- value._id :
- value;
- let castNumber;
- if (typeof this._castFunction === 'function') {
- castNumber = this._castFunction;
- } else if (typeof this.constructor.cast === 'function') {
- castNumber = this.constructor.cast();
- } else {
- castNumber = SchemaNumber.cast();
- }
- try {
- return castNumber(val);
- } catch (err) {
- throw new CastError('Number', val, this.path, err, this);
- }
- };
- function handleSingle(val) {
- return this.cast(val);
- }
- function handleArray(val) {
- const _this = this;
- if (!Array.isArray(val)) {
- return [this.cast(val)];
- }
- return val.map(function(m) {
- return _this.cast(m);
- });
- }
- SchemaNumber.prototype.$conditionalHandlers =
- utils.options(SchemaType.prototype.$conditionalHandlers, {
- $bitsAllClear: handleBitwiseOperator,
- $bitsAnyClear: handleBitwiseOperator,
- $bitsAllSet: handleBitwiseOperator,
- $bitsAnySet: handleBitwiseOperator,
- $gt: handleSingle,
- $gte: handleSingle,
- $lt: handleSingle,
- $lte: handleSingle,
- $mod: handleArray
- });
- SchemaNumber.prototype.castForQuery = function($conditional, val) {
- let handler;
- if (arguments.length === 2) {
- handler = this.$conditionalHandlers[$conditional];
- if (!handler) {
- throw new CastError('number', val, this.path, null, this);
- }
- return handler.call(this, val);
- }
- val = this._castForQuery($conditional);
- return val;
- };
- module.exports = SchemaNumber;
|