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

SimpleSchema.oneOf Issue (simple example included) #699

Closed zjzeit closed 7 years ago

zjzeit commented 7 years ago

Below is the example case. If I un-comment type: FirstSchema and comment out the next line, no error is produced. Thus: when the type is specifically FirstSchema there is not error but if the type is oneOf a list that includes FirstSchema, an error is produced, which is not the intent of oneOf (unless my understanding is mistaken). Shouldn't oneOf test for String, then Number, then FirstSchema, and pass once it tests FirstSchema to be a match?

// Import
import SimpleSchema from 'simpl-schema';

// Instantiate
Test = new Mongo.Collection('test');

// Define Schema
ZeroSchema = new SimpleSchema({
    zeroAlpha: Number,
    zeroBravo: Number
});
FirstSchema = new SimpleSchema({
    firstAlpha: String,
    firstBravo: String
});
SecondSchema = new SimpleSchema({
    secondAlpha: String,
    secondBravo: {
        //type: FirstSchema,
        type: SimpleSchema.oneOf(String, Number, FirstSchema, ZeroSchema)
    }
});

// Attach Schema
Test.attachSchema(SecondSchema);

// Sample Data
myData = {
    secondAlpha: "test",
    secondBravo: {
        firstAlpha: "test",
        firstBravo: "test"
    }
};
Test.insert(myData); // Error: secondBravo.firstAlpha is not allowed by the schema

Also interesting note: if I do type: SimpleSchema.oneOf(String, Number, FirstSchema), i.e., remove the ZeroSchema part, it validates/inserts successfully.

zjzeit commented 7 years ago

Migrating to node-simple-schema.