简体   繁体   中英

How to declare simpleschema OR within subschema Meteor js 1.8

In my data schema I would like to have a nested object in an object. The nested object should be validated as well, but be able to contain different types of data. So there are several schema's for the same object data.

To clarify this: Object > Object.data > [DataSchema A || DataSchema B || DataSchema C]. There is a variable in Object: Object.Type which corresponds to what data schema should be used.

How do I apply the correct nested schema to the object.

I have tried to google the problem and the function if's, don't seem to provide the functionality for this. Right now my solution is to have every variable in the subschemas optional but it's an ugly solution.

export const DataSchema1 = ({ /* some variables */ });
export const DataSchema2 = ({ /* some different variables */ });
export const DataSchema3 = ({ /* some different variables */ });

export const DataSchema = [
       DataSchema1,
       DataSchema2,
       DataSchema3
];


export const ObjectSchema = new SimpleSchema({
        objectType: {
            // Actually is enum, to verify options. Not relevant.
             type: String
        },
        data: {
            type: DataSchema
        }

});

Expect to be able to call the meteor check function to validate an object to this schema. Either based on the type variable or apply an or to the array of DataSchema.

Thank you guys in advance! First stackoverflow post.

Welcome to StackOverflow!

SimpleSchema has a helper called oneOf , which lets you compose types together:

export const ObjectSchema = new SimpleSchema({
    objectType: {
        // Actually is enum, to verify options. Not relevant.
         type: String,
         allowedValues: [/* you can put your enum values here */]
    },
    data: {
        type: SimpleSchema.oneOf([DataSchema1, DataSchema2, DataSchema3])
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM