arrays - Conditional Json Schema validation based on property value -
i have input json below,
{ "results": [ { "name": "a", "testa": "testavalue" } ] }
the condition is, if value of 'name' 'a', 'testa' should required field , if value of 'name' 'b', 'testb' should required field.
this json schema tried , not working expected,
{ "$schema": "http://json-schema.org/draft-04/schema#", "type": "object", "required": [ "results" ], "properties": { "results": { "type": "array", "oneof": [ { "$ref": "#/definitions/person" }, { "$ref": "#/definitions/company" } ] } }, "definitions": { "person": { "type": "object", "required": [ "name", "testa" ], "properties": { "name": { "type": "string", "enum": [ "a" ] }, "testa": { "type": "string" } } }, "company": { "type": "object", "required": [ "name", "testb" ], "properties": { "name": { "type": "string", "enum": [ "b" ] }, "testb": { "type": "string" } } } } }
tried "dependecies" in json schema wasn't able find correct solution.
any / workaround sample json schema achieve above use case appreciated.
your're close. oneof
needs in items
keyword.
{ "type": "array", "items": { "oneof": [ { "$ref": "#/definitions/person" }, { "$ref": "#/definitions/company" } ] } }
Comments
Post a Comment