How to add dependency #485
-
I would like to know how to add dependency on another property's value. For e.g. if I want B to be dependent on A, I can add below.
This works perfectly for me. Now, say A is a boolean and if I want to add a check that B should only be defined if A is |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Hi @vicky-blr, This sounds more like a general question regarding JSON Schema than how to produce that with the generator library. 😃 {
"type": "object",
"properties": {
"A": {"type": "boolean"}
},
"if": { "required": ["B"] },
"then": { "properties": { "A": { "const": true } }, "required": ["A"] },
} I favor this alternative though: https://tour.json-schema.org/content/05-Conditional-Validation/03-Conditionally-Apply-a-Subschema {
"type": "object",
"properties": {
"A": {"type": "boolean"}
},
"dependentSchemas": {
"B": { "properties": { "A": { "const": true } }, "required": ["A"] }
}
} Both have the same meaning: if a property "B" is present, then a property "A" must be present and have the value If you have any further questions on configuring your JSON Schema Generator to produce the desired output, I'm happy to help there as well. 😉 |
Beta Was this translation helpful? Give feedback.
Hi @vicky-blr,
This sounds more like a general question regarding JSON Schema than how to produce that with the generator library. 😃
For that, I suggest you have a look at: https://tour.json-schema.org/content/05-Conditional-Validation/04-if-then-keyword
By swapping the
if
andthen
subschema in their lesson's example you'd get pretty close to what you're describing as your use case:I favor this alternative though: https://tour.json-schema.org/content/05-Conditional-Validation/03-Conditionally-Apply-a-Subschema