This commit is contained in:
Jonasz Bigda
2023-03-25 21:51:42 +01:00
parent 0db1d5117e
commit b332e9ceb0
1044 changed files with 37502 additions and 63938 deletions

View File

@@ -11,10 +11,7 @@ const castNumber = require('../cast/number');
const handleBitwiseOperator = require('./operators/bitwise');
const utils = require('../utils');
const populateModelSymbol = require('../helpers/symbols').populateModelSymbol;
const CastError = SchemaType.CastError;
let Document;
/**
* Number SchemaType constructor.
@@ -103,18 +100,24 @@ SchemaNumber.cast = function cast(caster) {
return this._cast;
}
if (caster === false) {
caster = v => {
if (typeof v !== 'number') {
throw new Error();
}
return v;
};
caster = this._defaultCaster;
}
this._cast = caster;
return this._cast;
};
/*!
* ignore
*/
SchemaNumber._defaultCaster = v => {
if (typeof v !== 'number') {
throw new Error();
}
return v;
};
/**
* This schema type's name, to defend against minifiers that mangle
* function names.
@@ -179,9 +182,9 @@ SchemaNumber.prototype.checkRequired = function checkRequired(value, doc) {
*
* ####Example:
*
* var s = new Schema({ n: { type: Number, min: 10 })
* var M = db.model('M', s)
* var m = new M({ n: 9 })
* const s = new Schema({ n: { type: Number, min: 10 })
* const M = db.model('M', s)
* const m = new M({ n: 9 })
* m.save(function (err) {
* console.error(err) // validator error
* m.n = 10;
@@ -190,10 +193,10 @@ SchemaNumber.prototype.checkRequired = function checkRequired(value, doc) {
*
* // custom error messages
* // We can also use the special {MIN} token which will be replaced with the invalid value
* var min = [10, 'The value of path `{PATH}` ({VALUE}) is beneath the limit ({MIN}).'];
* var schema = new Schema({ n: { type: Number, min: min })
* var M = mongoose.model('Measurement', schema);
* var s= new M({ n: 4 });
* const min = [10, 'The value of path `{PATH}` ({VALUE}) is beneath the limit ({MIN}).'];
* const schema = new Schema({ n: { type: Number, min: min })
* const M = mongoose.model('Measurement', schema);
* const s= new M({ n: 4 });
* s.validate(function (err) {
* console.log(String(err)) // ValidationError: The value of path `n` (4) is beneath the limit (10).
* })
@@ -233,9 +236,9 @@ SchemaNumber.prototype.min = function(value, message) {
*
* ####Example:
*
* var s = new Schema({ n: { type: Number, max: 10 })
* var M = db.model('M', s)
* var m = new M({ n: 11 })
* const s = new Schema({ n: { type: Number, max: 10 })
* const M = db.model('M', s)
* const m = new M({ n: 11 })
* m.save(function (err) {
* console.error(err) // validator error
* m.n = 10;
@@ -244,10 +247,10 @@ SchemaNumber.prototype.min = function(value, message) {
*
* // custom error messages
* // We can also use the special {MAX} token which will be replaced with the invalid value
* var max = [10, 'The value of path `{PATH}` ({VALUE}) exceeds the limit ({MAX}).'];
* var schema = new Schema({ n: { type: Number, max: max })
* var M = mongoose.model('Measurement', schema);
* var s= new M({ n: 4 });
* const max = [10, 'The value of path `{PATH}` ({VALUE}) exceeds the limit ({MAX}).'];
* const schema = new Schema({ n: { type: Number, max: max })
* const M = mongoose.model('Measurement', schema);
* const s= new M({ n: 4 });
* s.validate(function (err) {
* console.log(String(err)) // ValidationError: The value of path `n` (4) exceeds the limit (10).
* })
@@ -287,10 +290,10 @@ SchemaNumber.prototype.max = function(value, message) {
*
* ####Example:
*
* var s = new Schema({ n: { type: Number, enum: [1, 2, 3] });
* var M = db.model('M', s);
* const s = new Schema({ n: { type: Number, enum: [1, 2, 3] });
* const M = db.model('M', s);
*
* var m = new M({ n: 4 });
* const m = new M({ n: 4 });
* await m.save(); // throws validation error
*
* m.n = 3;
@@ -310,8 +313,13 @@ SchemaNumber.prototype.enum = function(values, message) {
}, this);
}
if (!Array.isArray(values)) {
values = Array.prototype.slice.call(arguments);
if (utils.isObject(values)) {
values = utils.object.vals(values);
} else {
values = Array.prototype.slice.call(arguments);
}
message = MongooseError.messages.Number.enum;
}
@@ -339,45 +347,26 @@ SchemaNumber.prototype.enum = function(values, message) {
SchemaNumber.prototype.cast = function(value, doc, init) {
if (SchemaType._isRef(this, value, doc, init)) {
// wait! we may need to cast this to a document
if (value === null || value === undefined) {
return value;
}
// lazy load
Document || (Document = require('./../document'));
if (value instanceof Document) {
value.$__.wasPopulated = true;
return value;
}
// setting a populated path
if (typeof value === 'number') {
return value;
} else if (Buffer.isBuffer(value) || !utils.isObject(value)) {
throw new CastError('Number', value, this.path, null, this);
}
// Handle the case where user directly sets a populated
// path to a plain object; cast to the Model used in
// the population query.
const path = doc.$__fullPath(this.path);
const owner = doc.ownerDocument ? doc.ownerDocument() : doc;
const pop = owner.populated(path, true);
const ret = new pop.options[populateModelSymbol](value);
ret.$__.wasPopulated = true;
return ret;
return this._castRef(value, doc, init);
}
const val = value && typeof value._id !== 'undefined' ?
value._id : // documents
value;
const castNumber = typeof this.constructor.cast === 'function' ?
this.constructor.cast() :
SchemaNumber.cast();
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) {