Omgabee
Omgabee

Reputation: 117

Collection Insert within Collection-Hook bypasses SimpleSchema

I know there are issues opened over Collection-Hooks not working nicely with SimpleSchema. The issue seems to be that SimpleSchema runs before Collection-Hooks.

But in the following example, I am inserting a document into a completely different collection and it seems to bypass SimpleSchema all together. This was kind of surprising to me, because I thought since Logs is a different collection it would trigger it's own SimpleSchema still....but it doesn't the way I've done it here.

Does someone know of a workaround to allow Logs to use it's SimpleSchema for this example?

Products = new Mongo.Collection('products');

Products.after.insert((userId, doc) => {
    Logs.insert({'someinvalid': 'schema'});
    // logs still gets inserted here even though it's schema should be invalid
});

Logs = new Mongo.Collection('logs');

LogsSchema = new SimpleSchema({
    'someSchema': {
        type: String
    }
});

Logs.attachSchema(LogsSchema);

Upvotes: 1

Views: 78

Answers (1)

Omgabee
Omgabee

Reputation: 117

I figured it out on my own. All I needed to do was apply the check package and use:

Products.after.insert((userId, doc) => {
    let obj = {'someinvalid': 'schema'};

    check(obj, LogsSchema);

    Logs.insert(obj);
});

I forgot you can use SimpleSchema as a validation check outside of collections themselves ^^;

Upvotes: 1

Related Questions