Roeurb Navy
Roeurb Navy

Reputation: 91

Meteor validate object and clean object with simple schema

I have defined a schema for a meteor mongo collection using smpl-schema to validate and clean object.

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

data :

let doc = {
  name:' ',
  age:10,
}

Here my function to validate :

function validateData(doc){
let validationContext = schema.newContext();
validationContext.validate(doc);
if (!validationContext.isValid()) throw JSON.stringify(validationContext.validationErrors())

return true
}

It's error, it's output : Error: Cannot convert undefined or null to object [ValidateDataError]

Upvotes: 0

Views: 561

Answers (1)

faysvas
faysvas

Reputation: 1

You have a typo in your schema, optional should be set to true

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

Upvotes: 0

Related Questions