SchemaNumber
SchemaNumber()SchemaNumber.checkRequired()SchemaNumber.get()SchemaNumber.get()SchemaNumber.prototype.checkRequired()SchemaNumber.prototype.enum()SchemaNumber.prototype.max()SchemaNumber.prototype.min()SchemaNumber.schemaNameSchemaNumber.set()
SchemaNumber()
参数
key«字符串»options«对象»
继承
Number SchemaType 构造函数。
SchemaNumber.checkRequired()
参数
fn«函数»
返回
- «函数»
类型
- «属性»
覆盖用于检查字符串是否通过 `required` 检查的必需验证器使用的函数。
SchemaNumber.get()
参数
getter«函数»
返回
- «this»
类型
- «属性»
为所有 Number 实例附加一个 getter。
示例
// 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 SchemaNumber.get()
参数
caster«函数»
返回
- «函数»
类型
- «属性»
获取/设置用于将任意值转换为数字的函数。
示例
// 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); SchemaNumber.prototype.checkRequired()
参数
value«任何»doc«文档»
返回
- «布尔值»
检查给定值是否满足必需验证器。
SchemaNumber.prototype.enum()
参数
values«数组» 允许的值[message]«字符串» 可选的自定义错误消息
返回
- «SchemaType» this
参见
设置枚举验证器
示例
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 SchemaNumber.prototype.max()
参数
maximum«数字» 数字[message]«字符串» 可选的自定义错误消息
返回
- «SchemaType» this
参见
设置最大数字验证器。
示例
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).
}) SchemaNumber.prototype.min()
参数
value«数字» 最小数字[message]«字符串» 可选的自定义错误消息
返回
- «SchemaType» this
参见
设置最小数字验证器。
示例
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).
}) SchemaNumber.schemaName
类型
- «属性»
此模式类型的名称,以防御混淆函数名称的缩小器。
SchemaNumber.set()
参数
option«字符串» 您想为其设置值的选项value«任何» 选项的值
返回
- «未定义,空»
类型
- «属性»
为所有 Number 实例设置默认选项。
示例
// 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. 
