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

Popular posts from this blog

php - Zend Framework / Skeleton-Application / Composer install issue -

c# - Better 64-bit byte array hash -

python - PyCharm Type error Message -