oferei / json-gate

A friendly, fast JSON schema validator
MIT License
100 stars 16 forks source link

Requires at least one property #28

Closed andersonba closed 9 years ago

andersonba commented 10 years ago

I have object property in a schema that requires at least one property in the object.

See below:

{
   client: { required:true, ... },
   order: { required:true, ... }
   creditcard: { ... },
   token: { ... }
}

I need to validate if the object has a creditcard OR token property, is required at least one but not simultaneous (XOR).

Is it possible? How?

May be atLeastOne attribute resolve this:

{
   type: 'object',
   atLeastOne: ['creditcard', 'token'],
   ...
   creditcard: { ... },
   token: { ... }
}

Thanks!

oferei commented 9 years ago

hi. sorry for the late response. for some reason this question went under my radar...

I'm not sure how to solve this riddle. perhaps a "union type" can help. try placing the two optional fields inside a new child object and define that child using a union type with 2 schemas in it (one with only "creditcard" and one with only "token").

anyway, at this point I'm trying to avoid adding features that are not part of the standard. so I'm not going to add atLeastOne at this time.