Richard Michael Coo
Richard Michael Coo

Reputation: 120

Meteor simple-schema validation on empty instances of a class

I actually posted this on the simple-schema Github issues w/ no response. My code could be wrong, or my understanding of JS OOP could be wrong, as I come from a Java and PHP background. So I'm not sure if this is a bug, but it seems that:

check({}, Schema.User) would trigger an exception, since the empty object is lacking required fields, but check(new User(), Schema.User) would be valid

isn't new User() supposed to have no values for required fields as well?

Here's also a quick experiment I did:

it("does not validate correctly if i give it an instance", function() {
  var Bee, b, schema;
  schema = new SimpleSchema({
    name: {
      type: String,
      optional: false
    }
  });

  Bee = function() {};
  b = new Bee();

  schema.newContext().validate(b, {modifier: false}).should.be.false  // validate is true here
});

it("works if i give it {}", function() {
  var schema;
  schema = new SimpleSchema({
    name: {
      type: String,
      optional: false
    }
  });

  schema.newContext().validate({}, {modifier: false}).should.be.false  // validate is false here
});

Upvotes: 0

Views: 557

Answers (1)

Richard Michael Coo
Richard Michael Coo

Reputation: 120

I think this is intentional, and seems to validate only basic objects (so it validates {}, but not new Bee()) because of else if (Utility.isBasicObject(val) ... somewhere in the simple-schema-validation.js code.

Upvotes: 1

Related Questions