meteorsimple-schema

Unknown key in field XXX when using simpl-schema


I was using only aldeed:simple-schema in my Project with the packages check and audit-argument-checks. The check function using my SimpleSchema was working fine.

But then i wanted to use collection2. Collection2 need the npm package simpl-schema. When i installed aldeed:collection2 and the npm package simpl-schema, my checks using SimpleSchema stopped working and now show the following error:

Error: Match error: Unknown key in field title

Check() is working with aldeed:simple-schema but not with the npm package simpl-schema.

import SimpleSchema from 'simpl-schema';

NoteUpsertSchema = new SimpleSchema({
  title: {
    type: String,
    max: 50
  },
  description: {
    type: String,
    max: 500
  }
});

My Meteor Method

updateNote(noteId, note){
    check(noteId, String);
    check(note, NoteUpsertSchema);
    // some code
}

Versions of my packages:

// Meteor packages
aldeed:collection2         3.0.0  
audit-argument-checks      1.0.7 
check                      1.3.0* 

// Npm package
"simpl-schema": "^1.5.0"
(I tried with simpl-schema: 1.4.3 same result.)

How can i use the four packages check, audit-argument-checks, simple-schema and collection2 together ?

Thanks for your answer


Solution

  • You cannot use NoteUpsertSchema in check utility in Meteor.

    check, audit-argument-checks, simpl-schema and collection2 works very well in synch, there is no such issue with inter-compatibility. Check only allows there defined parameters against which you can crosscheck the validity. Click here to know the details of check allowed types.

    Considering audit-argument-checks, you need to use approach shown as an example below to check arguments passed in Meteor Method. To avoid errors about not checking all arguments when you are using SimpleSchema to validate Meteor method arguments, you must pass check as an option when creating your SimpleSchema instance.

    import SimpleSchema from 'simpl-schema';
    import { check } from 'meteor/check';
    import { Meteor } from 'meteor/meteor';
    
    SimpleSchema.defineValidationErrorTransform(error => {
      const ddpError = new Meteor.Error(error.message);
      ddpError.error = 'validation-error';
      ddpError.details = error.details;
      return ddpError;
    });
    
    const myMethodObjArgSchema = new SimpleSchema({ name: String }, { check });
    
    Meteor.methods({
      myMethod(obj) {
        myMethodObjArgSchema.validate(obj);
    
        // Now do other method stuff knowing that obj satisfies the schema
      },
    });
    

    Make sure the aldeed:simple-schema is not listed in .meteor/versions file.

    Also Problem can be sending a full object from client and only validating some of its fields within meteor method. Make sure the parameter being sent to the method only have what is being validated and no extra field from the client code.