具有object的下一个参数:
var mongoose = require (PATH);
var Schema = mongoose.Schema;
var schema - new Schema ({
barcode:{
type:number,
required:true,
unique:true
},...});
我想验证“条形码”,它将不少于也不超过14个字符;
为此,我编写了以下代码:
schema.path('barcode').validate(function(barcode){
return barcode.length == 13;
}, 'sorry, the error occurred, be careful while typing, 14 characters only!");
exports.Item = mongoose.model('Item', schema);
但是,当我将此模式实现到具体对象时,此验证不起任何作用。我的意思是,我可以输入任何长度的数字,并且没有任何错误发生!
发布于 2014-04-15 15:43:40
查看mongoose-validator。它与mongoose
集成以支持自定义验证。你可以像这样使用它。
var validate = require('mongoose-validator').validate;
var BarcodeSchema = new Schema({
code: {
type: String,
required: true,
unique: true,
validate: validate('len', 13)
}
});
https://stackoverflow.com/questions/23083930
复制