Meteor-Community-Packages / meteor-collection2

A Meteor package that extends Mongo.Collection to provide support for specifying a schema and then validating against that schema when inserting and updating.
https://packosphere.com/aldeed/collection2
MIT License
1.02k stars 108 forks source link

Unknown key in field XXX when using simpl-schema #381

Closed GaetanRouzies closed 3 years ago

GaetanRouzies commented 6 years ago

I was using only aldeed:simple-schema, but then i wanted to use collection2. 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() works with aldeed:simple-schema but not with simpl-schema. Is it normal ?

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
}

I looked inside the simpl-schema doc and i tried another validation method. Adding the parameter check as a second argument of my SimpleSchema and using mySchema.validate()

import SimpleSchema from 'simpl-schema';
import check from 'meteor/check';

NoteUpsertSchema = new SimpleSchema({
  title: {
    type: String,
    max: 50
  },
  description: {
    type: String,
    max: 500
  }
}, { check });
updateNote(noteId, note){
    check(noteId, String);
    NoteUpsertSchema.validate(note);
    // some code
}

But this code supposing to satisfy audit-argument-checks doesn't work cause it throws the following error:

Exception while invoking method 'updateNote' Error: Did not check() all arguments during call to 'updateNote'

Any idea how i could use check with collection2 and simpl-schema ? Thanks for your answer

GaetanRouzies commented 6 years ago

For the second part of my problem, i was importing check the wrong way. import check from 'meteor/check'; Should be: import { check } from 'meteor/check'; Then it satisfy audit-argument-checks

felixb101 commented 5 years ago

I have a similar issue, read somewhere that despite documentation, the only supported schema extensions was in the meta field.

StorytellerCZ commented 3 years ago

Please see: https://github.com/aldeed/simpl-schema/#validate-a-meteor-method-argument-and-satisfy-audit-argument-checks