jsonschema - Validate property against another with JSON Schema -
in model below, "category_id" property should required if "detail" array empty.
if "detail" array not empty, "category_id" property not required.
how can json schema?
{ "description": "expense model validation.", "type": "object", "properties": { "description": { "type": "string" }, "category_id": { "type": "string" }, "detail": { "type": "array", "items": { "description": "expense detail", "type": "object", "properties": { "description": { "type": "string" } }, "required": [ "description" ] } } }, "required": [ "description", "category_id" ] }
you can use anyof
check either category_id
present, or detail
present , has @ least 1 item.
{ "description": "expense model validation.", "type": "object", "properties": { "description": { "type": "string" }, "category_id": { "type": "string" }, "detail": { "type": "array", "items": { "description": "expense detail", "type": "object", "properties": { "description": { "type": "string" } }, "required": ["description"] } } }, "required": ["description"], "anyof": [ { "required": ["category_id"] }, { "properties": { "detail": { "minitems": 1 } }, "required": ["detail"] } ] }
Comments
Post a Comment