silfverorg / duns.js

Javascript schema validator
5 stars 2 forks source link

Optional keys for object. #41

Closed br0r closed 9 years ago

br0r commented 9 years ago

I want to validate just a single key in a big object which could have lots of different undefined keys.

I cannot do this today since the Object validator will throw when keys that are not specified in schema exists in object.

Therefore i propose a metod for the Object validator to make all keys allowed, and just do given validation.

Example:

var obj = {
  foo: {
    bar: {
      value: 5
      IWillbeCheckedAndWillThrow: true,
    },
    iWillNotBeCheckedEither: true,
  },
  iWillNotBeChecked: true,
};

var Schema = Duns.object().keys({
  foo: Duns.object().keys({
    bar: Duns.object().keys({
      value: Duns.number().max(6),
    }),
  }).allowAllKeys(),
}).allowAllKeys();

Duns.validate(obj, Schema) //Will return false since it will still will not validate the 'IWillbeCheckedAndWillThrow' key. But all other keys will be ok. 
silfverstrom commented 9 years ago

Seems like a good suggestion. +1