我已经使用smpl-schema定义了流星mongo集合的架构,以验证和清除对象。
import SimpleSchema from 'simpl-schema';
const schema = new SimpleSchema({
name:String,
age:Number,
address:{
type:String,
optional:True
}
}, {
clean: true,
});
数据:
let doc = {
name:' ',
age:10,
}
这是我要验证的功能:
function validateData(doc){
let validationContext = schema.newContext();
validationContext.validate(doc);
if (!validationContext.isValid()) throw JSON.stringify(validationContext.validationErrors())
return true
}
这是错误,它的输出是:Error: Cannot convert undefined or null to object [ValidateDataError]
答案 0 :(得分:0)
您的架构中有错字,optional
应该设置为true
import SimpleSchema from 'simpl-schema';
const schema = new SimpleSchema({
name:String,
age:Number,
address:{
type:String,
optional:true
}
}, {
clean: true,
});