JSON Schema conditional: field is required based on the value of another field

I am trying to implement this condition: a field is required based on the value of another field, that is, if a request with "index": "true" exists, then the element "id": true is required.

Here is an example circuit:

{ "$schema": "http://json-schema.org/draft-04/schema#", "title": "test title", "type": "object", "properties": { "data": { "type": "array", "items": { "$ref": "#/definitions/Item" }, "minItems": 0 } }, "required": [ "data" ], "definitions": { "Item": { "type": "object", "properties": { "id": { "type": [ "integer", "string" ] }, "type": { "type": "string" } } } } } 

How can this be implemented?

Any pointers would be helpful.

+6
source share
1 answer

Your chema sample is strange to me, but you can use this part of json shema to check the ticket description:

 "properties": { "data": { "oneOf": [ { "type": "object", "required": [ "index" ], "properties": { "index": { "type": "boolean", "enum": [false] } } }, { "type": "object", "required": [ "index", "id" ], "properties": { "index": { "type": "boolean", "enum": [true] }, "id": { "type": "boolean" } } } ] } } 

This trick helps you if you want to check one parameter when the other parameters are equal to some value.

+7
source

Source: https://habr.com/ru/post/978583/


All Articles