I’ve looked through the previous questions and can’t seem to find the specific scenario, so apologies if you’ve seen this question before.
For brevity sake, I have the follow JSON
{
"minimum": 123,
"charges":[
{"amount": 123, "billable": 456},
{"amount": 234, "billable": 456},
]
}
- "minimum" is optional initially,
- both "amount" and "billable" are optional
- If "amount" is present in any of the objects in the "charges" array, then "minimum" is now required.
The following would be invalid:
{
"charges":[
{"billable": 456},
{"amount": 234, "billable": 456}, // amount is present here, so "minimum" needs to be on the root node.
]
}
The following is valid:
{
"charges":[
{"billable": 456},
{"billable": 456},
]
}
Is this behavior possible? Thank you, brain trust.
2
Answers
Yes, it is possible. But I think that you will need to validate the schema by hand with some kind of function like this:
here you can test the function with your 2 examples:
Given your requirements you would need to use the
if
andrequired
keywords.