流星使用简单的模式验证对象和清除对象

时间:2019-05-08 10:09:41

标签: javascript meteor simple-schema

我已经使用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]

1 个答案:

答案 0 :(得分:0)

您的架构中有错字,optional应该设置为true

import SimpleSchema from 'simpl-schema';
const schema = new SimpleSchema({
   name:String,
   age:Number,
   address:{
   type:String,
   optional:true
   }
}, {
   clean: true,
});