Frido Emans
Frido Emans

Reputation: 5578

validating custom object in Meteor Simpleschema

I am trying to validate an object using SimpleSchema in Meteor, before inserting it into the database.

The object looks like this, as I print it from the Meteor method that calls the insert:

channels: { '1': [ 'rect4557-6-4-5-7-4-2', 'rect4557-6-4-97-0-7-6-3' ] } }

If I insert it in the database without attaching a schema to it, it works fine. However, when I have it run through SimpleSchema, the field value as outputted from console.log in the custom validation method is an empty object {}. Even if I don't run any validation, an empty object is stored if SimpleSchema is used.

Code to produce the simpleschema value output:

Arch.schema = new SimpleSchema({   
    channels: {
    type: Object,
    custom: function validateChannels() {
      console.log("this.value:", this.value)
    } 
}); 
Architectures.attachSchema(Architectures.schema);

Really, what should I do? Is this a bug in SimpleSchema?

Upvotes: 0

Views: 941

Answers (1)

dairystatedesigns
dairystatedesigns

Reputation: 392

It looks like you just need to add blackbox: true option. SimpleSchema does not support arbitrary object keys unless you mark it as a blackbox object. See https://github.com/aldeed/meteor-simple-schema#blackbox

The filtering, which is part of the automatic cleaning, is what strips this out for you. If you want to prevent that in a specific insert call, just pass filter: false option. See https://github.com/aldeed/meteor-collection2#skip-removing-properties-that-are-not-in-the-schema

Upvotes: 2

Related Questions