Reputation: 38
Goal: Either of prop1 or prop2 is required, and prop3,prop4 are always required
Issue: I tried validating json request using below json schema, but I was only able to validate for prop3 and prop4.
Json Schema:
{
'type':'object',
'properties':{
'prop1':{'type':'string'},
'prop2':{'type':'string'},
'prop3':{'type':'string','required':true},
'prop4':{'type':'string','required':true}
},
'additionalProperties':false,
'anyOf':[{'required':['prop1']},{'required':['prop2']}]
}
testJson1: { "prop2":"fdsd", "prop3":"101655", "prop4":"E8CD6fghggg" } Note: This is good: testJson2 { "prop3":"101655", "prop4":"E8CD6fghggg" } Note: This is valid too, but expected invalid.
Upvotes: 2
Views: 1730
Reputation: 13635
You are using two different ways to express required properties:
For 'prop3' and 'prop4' you are using json-schema Draft V3 way to specify required.
'prop3':{'required':true},
For 'prop1' and 'prop2' you are using json-schema Draft v4 (required is an array).
'required':['prop1']
You might be using Newtonsoft Json.Net, which uses Draft v3. That is the reason you only get 'prop3' and 'prop4) work properly. So you need to change the specification of required to V3 or V4 in a consistent way, and then choose a suitable validator.
Upvotes: 0