0
votes

I have recently discovered the if-then-else keywords that are available in JSON Schema 07.

I'd like to use these keywords to change the type of a property dependent upon the answer of another property.

Essentially I'd like to do:

"if": {
                "properties" : {
                  "QuestionA": {"enum": ["CorrectAnswer"]}
                }
              },
              "then": {
                "QuestionB": //Change type of question B from null to string 
              }
}

The whole point of doing this is so that when a question depends on the answer of a previous question being correct, then that question cannot be answered until the first question is asked correctly.

If I first set the second question's type to null, no answer can be given. Once the first question has been answered, the type of the second question is changed to string and the second question can be answered.

Is this achievable in JSON Schema using if then else?

1
Possibly related question: stackoverflow.com/q/53206547/2811843 ?PsychoFish
Did you need anything further on this, or are you able to accept an answer?Relequestual

1 Answers

3
votes

Yes.

The then keyword value must be a JSON Schema itself.

If the if schema validates successfully, the then schema is applied.

When "if" is present, and the instance successfully validates against its subschema, then valiation succeeds against this keyword if the instance also successfully validates against this keyword's subschema.

https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.6.2