1

I'm using aldeed:collection2 and aldeed:simple-schema packages. I want to validate a doc against the schema. My schema contains e.g. a string field with allowedValues array and an array of nested object, described with sub-schema. Like this:

...

type: {
    type: String,
    allowedValues: [ 'A', 'B', 'C' ],
    defaultValue: 'A',
    index: 1,
  },
nestedStuff: {
    type: [ new SimpleSchema(nestedStuffSchema.schema(Meteor, SimpleSchema)) ],
    defaultValue: [],
  },

...

I have a 'bad' doc which has e.g. "D" in type field and invalid nested array items.

At client I'm trying to:

Contacts.simpleSchema().namedContext().validate(badDoc);

and it returns true. SimpleSchema says the doc is valid even though its fields do not abide to schema. Validating 'bad' type field individually also returns true. What am I doing wrong? Why could SimpleSchema assume random stuff to be valid?

tristantzara
  • 5,597
  • 6
  • 26
  • 40

1 Answers1

0

if you want to validate an array of strings you need to keep String inside [ ].See the below code it may help

type: {
     type: [String],
     allowedValues: [ 'A', 'B', 'C' ],
     defaultValue: ['A'],
     index: 1,
   },
nestedStuff: {
     type: [ new SimpleSchema(nestedStuffSchema.schema(Meteor,SimpleSchema)) ],
defaultValue: [],
  },

Thanks

khem poudel
  • 587
  • 7
  • 13