key «String» options «Object» Number SchemaType constructor.
fn «Function» Override the function the required validator uses to check whether a string passes the required check.
getter «Function» Attaches a getter for all Number instances.
// Make all numbers round down
mongoose.Number.get(function(v) { return Math.floor(v); });
const Model = mongoose.model('Test', new Schema({ test: Number }));
new Model({ test: 3.14 }).test; // 3
caster «Function» Get/set the function used to cast arbitrary values to numbers.
// Make Mongoose cast empty strings '' to 0 for paths declared as numbers
const original = mongoose.Number.cast();
mongoose.Number.cast(v => {
if (v === '') { return 0; }
return original(v);
});
// Or disable casting entirely
mongoose.Number.cast(false);
value «Any» doc «Document» Check if the given value satisfies a required validator.
values «Array» allowed values [message] «String» optional custom error message Sets a enum validator
const s = new Schema({ n: { type: Number, enum: [1, 2, 3] });
const M = db.model('M', s);
const m = new M({ n: 4 });
await m.save(); // throws validation error
m.n = 3;
await m.save(); // succeeds
maximum «Number» number [message] «String» optional custom error message Sets a maximum number validator.
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;
m.save() // success
})
// custom error messages
// We can also use the special {MAX} token which will be replaced with the invalid value
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).
})
value «Number» minimum number [message] «String» optional custom error message Sets a minimum number validator.
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;
m.save() // success
})
// custom error messages
// We can also use the special {MIN} token which will be replaced with the invalid value
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).
})
This schema type's name, to defend against minifiers that mangle function names.
option «String» The option you'd like to set the value for value «Any» value for option Sets a default option for all Number instances.
// Make all numbers have option `min` equal to 0.
mongoose.Schema.Number.set('min', 0);
const Order = mongoose.model('Order', new Schema({ amount: Number }));
new Order({ amount: -10 }).validateSync().errors.amount.message; // Path `amount` must be larger than 0.
© 2010 LearnBoost
Licensed under the MIT License.
https://mongoosejs.com/docs/api/schemanumber.html