Almog
Almog

Reputation: 2837

Meteor method error - allowed by the schema [validation-error]

I'm getting the following error and have no idea why I'm pretty sure I'm doing everything correctly. I'm passing an array of objects and validating against that.

Method

export const insert = new ValidatedMethod({
    name: 'lineups.insert',

    // Validation
    validate(args) {
        console.log(args);
        new SimpleSchema({
            business: {  type: [Object] },
            competitors: { type: [Object] },
            location: { type: Object },
            type: { type: String }
        }).validate(args)
    },

    // Run
    run({ lineupsId}) {

        const loggedInUser = Meteor.user();

        // check user roles
        if (!loggedInUser) {
            throw new Meteor.Error(403, 'Access Denied');
        }
    }
}); 

Here are the arg that the method gets

Object {business: Array[1], competitors: Array[1], location: Object, type: "Business Analysis"}

But I'm getting the following error business.0.placeID is not allowed by the schema [validation-error]

Calling the method

let businessArray = $.map(Session.get('business'), function(value, index) {
            return [{placeID:value.place_id, name:value.name}];
        });

insert.call({
            business:  businessArray,
            competitors:  Session.get('competitors'),
            location: Session.get('location'),
            type:Session.get('type')
        }, (err, res) => {
            if (err) {
                if (ValidationError.is(err)) {
                    toastr.error(err.message);
                }
            } else {
                toastr.error('Success, lineup have been created');
            }
        });

Upvotes: 0

Views: 433

Answers (2)

RSamurai
RSamurai

Reputation: 23

Try this,

validate(args) {
    console.log(args);
    new SimpleSchema({
        business: {  blackbox: true, type: [Object] },
        competitors: { blackbox: true, type: [Object] },
        location: { blackbox: true, type: Object },
        type: { type: String }
    }).validate(args)
}, 

Upvotes: 0

Almog
Almog

Reputation: 2837

So the issue was with the validation you need to also validate each object value.

Which can be done like so

validate: new SimpleSchema({
        type: { type: String },
        business: { type: [Object] },
        'business.$.name': { type: String },
        'business.$.placeID': { type: String }
    }).validator(),  

Another option is like so, but with this I could not get the method run to execute.

validate(args) {
        new SimpleSchema({
            business: [{ placeID: String, name: String }],
            competitors: { type: [Object] },
            location: { type: Object },
            type: { type: String }
        }).validator(args)
    },

Upvotes: 3

Related Questions