0
votes

So up until this point my experience with Collection2/Simple Schema has been basic at best. Now, I'm trying to validate values for 'types' based on documents in a Type collection.

@Data = new Mongo.Collection 'data'
Data.attachSchema new SimpleSchema
    'types':
        'type': [String]
        'label': 'Types'
        'custom': ->
            if @isSet
                Meteor.call 'isType', @value, (error, result) ->
                    if !result
                        Data.simpleSchema().namedContext('admin_update').addInvalidKeys [
                            'name': 'types'
                            'type': 'notAllowed'
                        ]
    'otherField':
        'type': Number
        'label': 'Other Field'
        'optional': true

So far, my isType Method has been validating the values correctly, but regardless of whether it returns true or false, it stores the value anyway (even though the form flashes the error message briefly). I don't think I grasp custom validation well enough to figure out how to do this correctly, so any and all help would be appreciated, even if it's just pushing me in the right direction.

1

1 Answers

0
votes

You can use the allowedValues field, which can take an array or a function. Use a function.

MyCollection.attachSchema(new SimpleSchema({
  types: {
    type: [String],
    allowedValues: function () {
      // assuming "Types" is the collection.
      return Types.find().map(function (doc) {
        return doc._id;
      });
    }
  }
}));

This validates on the back end so the publications and subscriptions will not be a problem.