Could I use dynamic simple-schema on one Collection?

import {Mongo} from 'meteor/mongo';
import {SimpleSchema} from 'meteor/aldeed:simple-schema';

export const Client = new Mongo.Collection("client");

// Optional is ture
Schema1 = new SimpleSchema({
    name: {
        type: String,
        label: 'Name',
        optional: true,
    }
......
});
---------
// Required
Schema2 = new SimpleSchema({
    name: {
        type: String,
        label: 'Name',
    }
......
});

// Check setting collection
.....
if(getSetting.optional === true){
   Client.attachSchema(Schema1)
}else{
   Client.attachSchema(Schema2)
}

Please help me.

Check out attaching of multiple schemas, that might be the better solution

I also see that you are using atmosphere version of SimpleSchema.

There is an npm package called simpl-schema by aldeed with up to date features.

I recommend you to take a look at the following link change log

1 Like

Thanks, I will try :smile: