Meteor-Community-Packages / meteor-simple-schema

Meteor integration package for simpl-schema
https://github.com/Meteor-Community-Packages/meteor-simple-schema
MIT License
920 stars 162 forks source link

Synchronous addInvalidKeys does not prevent successfull validation #696

Closed Kshatra closed 7 years ago

Kshatra commented 7 years ago

I have a simple schema for equality check on 2 fields:

const checkEqualSchema = new SimpleSchema({
    field1: {
        type: String,
    },
    field2: {
        type: String,
        custom() {
            if ( this.field('field1').value != this.value ) {
                checkEqualSchema.namedContext('context1').addInvalidKeys([
                    {name: "field1", type: "fieldsMustMatch"},
                    {name: "field2", type: "fieldsMustMatch"}
                ]);
            }
        }
    },
});

var doc  = {
    field1: 'value1',
    field2: 'value2'
},
ssContext = checkEqualSchema.namedContext("context1");

ssContext.validate(doc);

console.log('isValid: ',ssContext.isValid()); // true

Why is doc considered valid even if i add invalid keys in "custom" schema field?