Implement if/then/else syntax for conditional fields
See original GitHub issuePrerequisites
- I have read the documentation;
- In the case of a bug report, I understand that providing a SSCCE example is tremendously useful to the maintainers.
Description
The dependencies syntax is very powerful, however limitations are very quickly hit when dealing with fields across different objects.
A usecase we have internally is:
when: entity.legal_status != ‘limited_company’
then: ask for applicant.address
As additional incentive, there is a warning in the spec that dependencies could disappear.
Expected behavior
based on the example in the readme:
This can be written as chained if/then/else statements:
{
"if": {"properties": {"Do you have any pets?": {"enum": ["No"]}}},
"else": {
"if": {"properties": {"Do you have any pets?": {"enum": ["Yes: One"]}}},
"then": {
"properties": {"How old is your pet?": {"type": "number"}},
"required": ["How old is your pet?"]
},
"else": {
"...": "etc"
}
}
}
or using a oneOf:
{
"oneOf": [
{
"if": {"properties": {"Do you have any pets?": {"enum": ["No"]}}},
"else": false
},
{
"if": {"properties": {"Do you have any pets?": {"enum": ["Yes: One"]}}},
"then": {
"properties": {"How old is your pet?": {"type": "number"}},
"required": ["How old is your pet?"]
},
"else": false
},
"..."
]
}
the false on else is to force the validity to evaluate falsely, so oneOf moves on
While implementing this, I came across a potential issue, in that utils.retrieveSchema doesn’t do a deep merge when resolving $refs.
Given that the if/then/else syntax opens up the possibilities of doing cross-object conditioning, is this something that might need fixing?
The main issue caused was that lists would not get appended, meaning if a $ref defined required, it would get discarded in favour of applied within a conditional.
If this is functionality people want here, I can prep a pull request.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:13
- Comments:8 (2 by maintainers)

Top Related StackOverflow Question
@cybojenix, sorry for intrusion, but is there any way you can share your fork, or just that part of the code where you implemented that.
@epicfaace : It looks like that PR has merge conflicts. Do those need to be resolved before consideration? (I don’t work in this project, so I apologize if this question is not applicable).