Meteor.method Simple Schema Validation

Hello,

When I call methor method, Meteor Guide sugests a validation, using SimpleSchema. SimpleSchema docs allow me to use a custom class, in this case Rally. Whenever I do the validation, console gives me Internal Server Error: 500.
My server log gives me:

Exception while invoking method 'rallyGenerateTracks' TypeError: (intermediate value).validade is not a function

Here is my Method:

Meteor.methods({
    'rallyGenerateTracks':function(rallyObj){
        new SimpleSchema({
            rallyObj: {type: Rally}
        }).validade({ rallyObj });
         rallyType = rallyObj.regularity;
         return rallyType;
    }
});

And my Call:

// Used to create tracks on rally
Rally.after.insert(function (userId, doc) {
    Meteor.call("rallyGenerateTracks", {rallyObj: doc}, function(error, result){
        if(error){
            console.log("error", error);
        }
        if(result){
            console.log(this.rallyType);
        }
    });
  
});

I supose it is not working because Collection Hooks are passing only a Json, not the actual Rally Object. Tried to validate with a Json, but no success either…

Any help would be appreciated.

I think you want validate.

1 Like

Oh… That was dumb… Thanks for the fast reply.

Still, ValidaTion is not working.
It seems to be iterating over every Rally.Key. Server log gives me:

details: 
rally-meteor | I20181205-13:14:03.192(0)?    [ { name: 'rallyObj.rallyObj',
rally-meteor | I20181205-13:14:03.193(0)?        type: 'keyNotInSchema',
rally-meteor | I20181205-13:14:03.193(0)?        value: [Object],
rally-meteor | I20181205-13:14:03.193(0)?        message: 'rallyObj.rallyObj is not allowed by the schema' },

You seem to be doing some strange (to me) object conversions between the Meteor.call and the method itself. Also, you’re not logging the returned result.

Without understanding what you’re trying to do here(!), I would have expected something like:

Meteor.methods({
  rallyGenerateTracks(rallyObj) {
    new SimpleSchema({
      rallyObj: { type: Rally }
    }).validaTe(rallyObj);
    rallyType = rallyObj.regularity;
    return rallyType;
  }
});

Rally.after.insert(function(userId, doc) {
  Meteor.call('rallyGenerateTracks', doc, (error, result) => {
    if (error) {
      console.log('error', error);
    }
    if (result) {
      console.log(result);
    }
  });
});

It is still not working.
Would be a bad pattern to pass Rally._id as parameter and search for it again in the meteor.method using findOne?

Same error here:

rally-meteor | I20181205-13:34:24.796(0)?   errorType: 'ClientError',
rally-meteor | I20181205-13:34:24.796(0)?   name: 'ClientError',
rally-meteor | I20181205-13:34:24.796(0)?   error: 'validation-error',
rally-meteor | I20181205-13:34:24.796(0)?   details: 
rally-meteor | I20181205-13:34:24.797(0)?    [ { name: 'myrally',
rally-meteor | I20181205-13:34:24.797(0)?        type: 'keyNotInSchema',
rally-meteor | I20181205-13:34:24.797(0)?        value: [Object],
rally-meteor | I20181205-13:34:24.797(0)?        message: 'myrally is not allowed by the schema' },
rally-meteor | I20181205-13:34:24.798(0)?      { name: 'myrally._id',
rally-meteor | I20181205-13:34:24.798(0)?        type: 'keyNotInSchema',
rally-meteor | I20181205-13:34:24.798(0)?        value: 'Gq4s32oSneQL6H9Lr',
rally-meteor | I20181205-13:34:24.799(0)?        message: 'myrally._id is not allowed by the schema' },
rally-meteor | I20181205-13:34:24.799(0)?      { name: 'myrally.rallyName',
rally-meteor | I20181205-13:34:24.799(0)?        type: 'keyNotInSchema',
rally-meteor | I20181205-13:34:24.799(0)?        value: 'Rally 2019',
rally-meteor | I20181205-13:34:24.800(0)?        message: 'myrally.rallyName is not allowed by the schema' },
rally-meteor | I20181205-13:34:24.800(0)?      { name: 'myrally.rallyInitials',
rally-meteor | I20181205-13:34:24.800(0)?        type: 'keyNotInSchema',
rally-meteor | I20181205-13:34:24.800(0)?        value: 'id2019',
rally-meteor | I20181205-13:34:24.801(0)?        message: 'myrally.rallyInitials is not allowed by the schema' },
rally-meteor | I20181205-13:34:24.801(0)?      { name: 'myrally.regularity',
rally-meteor | I20181205-13:34:24.801(0)?        type: 'keyNotInSchema',
rally-meteor | I20181205-13:34:24.801(0)?        value: 'Novo Testamento - 6meses',
rally-meteor | I20181205-13:34:24.802(0)?        message: 'myrally.regularity is not allowed by the schema' },
rally-meteor | I20181205-13:34:24.802(0)?      { name: 'myrally.startDate',
rally-meteor | I20181205-13:34:24.802(0)?        type: 'keyNotInSchema',
rally-meteor | I20181205-13:34:24.802(0)?        value: 2018-11-10T00:00:00.000Z,
rally-meteor | I20181205-13:34:24.803(0)?        message: 'myrally.startDate is not allowed by the schema' },
rally-meteor | I20181205-13:34:24.803(0)?      { name: 'myrally.regularityFlag',
rally-meteor | I20181205-13:34:24.803(0)?        type: 'keyNotInSchema',
rally-meteor | I20181205-13:34:24.803(0)?        value: [Array],
rally-meteor | I20181205-13:34:24.804(0)?        message: 'myrally.regularityFlag is not allowed by the schema' },
rally-meteor | I20181205-13:34:24.804(0)?      { name: 'myrally.regularityFlag.0',
rally-meteor | I20181205-13:34:24.804(0)?        type: 'keyNotInSchema',
rally-meteor | I20181205-13:34:24.804(0)?        value: [Object],
rally-meteor | I20181205-13:34:24.805(0)?        message: 'myrally.regularityFlag.0 is not allowed by the schema' },
rally-meteor | I20181205-13:34:24.805(0)?      { name: 'myrally.regularityFlag.0.name',
rally-meteor | I20181205-13:34:24.805(0)?        type: 'keyNotInSchema',
rally-meteor | I20181205-13:34:24.805(0)?        value: 'Socorro',
rally-meteor | I20181205-13:34:24.806(0)?        message: 'myrally.regularityFlag.0.name is not allowed by the schema' },
rally-meteor | I20181205-13:34:24.806(0)?      { name: 'myrally.regularityFlag.0.date',
rally-meteor | I20181205-13:34:24.806(0)?        type: 'keyNotInSchema',
rally-meteor | I20181205-13:34:24.806(0)?        value: 2018-11-16T00:00:00.000Z,
rally-meteor | I20181205-13:34:24.807(0)?        message: 'myrally.regularityFlag.0.date is not allowed by the schema' },
rally-meteor | I20181205-13:34:24.807(0)?      { name: 'myrally.regularityFlag.1',
rally-meteor | I20181205-13:34:24.807(0)?        type: 'keyNotInSchema',
rally-meteor | I20181205-13:34:24.807(0)?        value: [Object],
rally-meteor | I20181205-13:34:24.808(0)?        message: 'myrally.regularityFlag.1 is not allowed by the schema' },
rally-meteor | I20181205-13:34:24.808(0)?      { name: 'myrally.regularityFlag.1.name',
rally-meteor | I20181205-13:34:24.808(0)?        type: 'keyNotInSchema',
rally-meteor | I20181205-13:34:24.808(0)?        value: 'Assim',
rally-meteor | I20181205-13:34:24.808(0)?        message: 'myrally.regularityFlag.1.name is not allowed by the schema' },
rally-meteor | I20181205-13:34:24.809(0)?      { name: 'myrally.regularityFlag.1.date',
rally-meteor | I20181205-13:34:24.809(0)?        type: 'keyNotInSchema',
rally-meteor | I20181205-13:34:24.809(0)?        value: 2018-11-21T00:00:00.000Z,
rally-meteor | I20181205-13:34:24.809(0)?        message: 'myrally.regularityFlag.1.date is not allowed by the schema' },
rally-meteor | I20181205-13:34:24.810(0)?      { name: 'rallyObj',
rally-meteor | I20181205-13:34:24.810(0)?        type: 'required',
rally-meteor | I20181205-13:34:24.810(0)?        value: undefined,
rally-meteor | I20181205-13:34:24.810(0)?        message: 'Rally obj is required' }

Not a bad pattern.

However, it still looks like there’s some mismatch between what you’re expecting and what you’re testing. What does your Rally schema look like?

Here is my Rally Collection:

RallySchema = new SimpleSchema({
    rallyName: {
        type: String,
        label: "Rally",
        max: 200
    },
    rallyInitials: {
        type: String,
        label: "Id do Rally",
        max: 15
    },

    regularity: {
        type: String,
        label: "Modalidade",
        allowedValues: ['BĂ­blia - 1 ano', 'Novo Testamento - 6meses', 'Novo Testamento 5x5', 'Novos Convertidos'],
        autoform: {
            options: [
                {label: "BĂ­blia em 1 ano", value: "BĂ­blia - 1 ano"},
                {label: "Novo Testamento em 6 meses", value: "Novo Testamento - 6meses"},
                {label: "Novo Testamento 5x5", value: "Novo Testamento 5x5"},
                {label: "Novos Convertidos 30 dias", value: "Novos Convertidos"}
            ]
        }
    },

    startDate: {
        type: Date,
        label: "Largada",
    },

    regularityFlag: {
        type: Array,
        optional: true,
        label: "Bandeiradas",
        maxCount: 5
    },
    "regularityFlag.$": {
        type: Object,
        optional: true,
        label: "Bandeirada",
    },
    "regularityFlag.$.name": {
        type: String,
        maxCount: 20
    },
    "regularityFlag.$.date": {
        type: Date,
        label: "Data"
    },
    tracks: {
        type: Array,
        optional: true,
        label: "Trilhas",
    },
    'tracks.$': Object,
    'tracks.$.month': {
        type: String,
        optional: true
    },
    'tracks.$.references': {
        type: String,
        optional: true
    }
});

Rally.attachSchema(RallySchema);

It looks to me as if you want to validate(rallyObj.myrally)

Thanks but it is not working either…

No idea what am I doing wrong.

This Did Not worked as I though it would be.

I exported the SimpleSchema from Rally.js, and used validator(rally) validate the Rally Object.

import { RallySchema } from './rally.js';

Meteor.methods({
    rallyGenerateTracks(rally) {
       
        RallySchema.validator(rally);
        rallyType = rally.regularity;
        return rallyType;
    }
});

As you mentioned, I think there was a problem with types i was using in the new schema… No sure why it would not work before though.

Thanks for your assistance.

1 Like

I manage to understand and do the validation the right way.

I will post it here for future reference on using SimpleSchema to validate a custom class.

A small detail… Had to use keys:Array to pass the validation. Otherwise, SimpleSchema would give me message: '_id is not allowed by the schema'. Found reference here.

Meteor.methods({
    rallyGenerateTracks(rally) {
        SimpleSchema.validate(rally, RallySchema, {keys: ["rallyName", "regularity", "startDate"]});

        rallyType = rally.regularity;
        return rallyType;
    }
});
1 Like