What is the purpose of putting SimpleSchema.validate() into Tracker.autorun?


#1

I am using Collection 2 and simpl schema npm package. At the moment I am thinking about using the mdg:validated-method package. I explored the code, and I was not able to understand the below code.

Please could anyone help me out here?

Thanks

Template.Lists_show.onCreated(function listShowOnCreated() {
  this.autorun(() => {
    new SimpleSchema({
      list: { type: Function },
      todosReady: { type: Boolean },
      todos: { type: Mongo.Cursor },
    }).validate(Template.currentData());
  });


#2

That’s not a validated method, but to answer your question, anything in a Tracker will re-run if any reactive data inside changes. In that example, the reactive data is the cursor. So, basically, if the todo list changes, it will be revalidated.


#3

Just my opinion, but this pattern is a pretty nice Blaze alternative to propTypes. Really mostly helpful for catching type or data issues.


#4

If I pass my whole document as one parameter like so

insertDocument.call(doc)

how can I validate then the doc holding all the fields? I just want to avoid write to much code and do not want to copy my existing collection definition. I just want to validate against my existing collection with MyCollection.validate(doc). Is that possible, or do I have to mention now every parameter in my method again.

At the moment I validate at the client simply with this MyCollection.validate(doc);

// Validation function for the arguments. Only keyword arguments are accepted,
  // so the arguments are an object rather than an array. The SimpleSchema validator
  // throws a ValidationError from the mdg:validation-error package if the args don't
  // match the schema
  validate: new SimpleSchema({
    listId: { type: String },
    name: { type: String },
    desc: { type: String },
  }).validator(),