I know this question exists here, but I tried every solution, but didnt work.
I have schema which has two properties, and second property will be displayed when first property is true. For this I used “options/dependencies”. I want to make second property required when first property is true.
"authorizationNumber": {
"type": "object",
"format": "grid-strict",
"required": ["firstProperty"],
"properties": {
"firstProperty": {
"type": "boolean",
"default": false,
"options": {
"grid_columns": 3
}
},
"secondProperty": {
"type": "array",
"format": "select2",
"uniqueItems": true,
"minItems": 1,
"items": {
"type": "string",
"$ref": "#/definitions/serviceCode"
},
"options": {
"dependencies": {
"firstProperty": true
},
"select2": {
"closeOnSelect": true,
"tags": false
},
"grid_columns": 4
}
}
}
}
I tried if/then/else as below:
"authorizationNumber": {
"type": "object",
"format": "grid-strict",
"required": ["firstProperty"],
"if": {
"properties": {
"authorizationNumberAvailable":{
"const": true
}
}
},
"then": {
"required": ["secondProperty"]
},
"properties": {
"firstProperty": {
"type": "boolean",
"default": false,
"options": {
"grid_columns": 3
}
},
"secondProperty": {
"type": "array",
"format": "select2",
"uniqueItems": true,
"minItems": 1,
"items": {
"type": "string",
"$ref": "#/definitions/serviceCode"
},
"options": {
"dependencies": {
"firstProperty": true
},
"select2": {
"closeOnSelect": true,
"tags": false
},
"grid_columns": 4
}
}
}
}
This does not give any errors, but it is not setting the “secondProperty” as required.
I also tried wrapping if/then with “allOf” as it is suggested one of the answers. To conclude, “secondProperty” should be available and marked as required when “firstProperty” is set to true.