Andrii Yasynovyi
Andrii Yasynovyi

Reputation: 89

Meteor SimpleSchema does not throw error

I'm trying to implement checks in Meteor method, but facing some strange behavior of SimpleSchema package (i'm using latest one which is 1.3.3 at the moment);

From the Docs:

Call mySimpleSchema.validate(doc) to validate doc against the schema and throw a ValidationError if invalid. This is like check(doc, mySimpleSchema) but without the check dependency and with the ability to pass full schema error details back to a callback on the client.

I defining a simple Schema like this:

var mySchema = new SimpleSchema({ name: {type: String} });

var invalidDoc = { name: 123 };

However mySchema object does not have "validate" method. I can only call validation using

mySchema.namedContext().validate(invalidDoc);

This method return false, but didn't throw any exception. The only way to go about I found so far is to call Meteor "check" function like this: check(invaidDoc, mySchema) And this does work as expected.

So, my question is: how to perform checking of object in Meteor method context, without using check();

Meteor.methods({
  'myMethod'(someObject) {

    var schema = new SimpleSchema({
        name: { type: String }
    });

    schema.namedContext().validate(someObject); //DOES NOT THROW!

    console.log('This should not be here!');
  }
});

Upvotes: 0

Views: 379

Answers (1)

SylvainB
SylvainB

Reputation: 4820

This feature has apparently been added in version 1.4.0 of the SimpleSchema package, so it is not available yet in v1.3.3. I guess you will have to use check() until 1.4.0 hits atmosphere!

Upvotes: 0

Related Questions