Open mfulton26 opened 7 years ago
Note that as of draft-07 there are other mechanisms, per #40 in this repo.
In draft-08, dependencies
will split- the schema form used here will (almost certainly) still be called dependencies
, while the string array form will get a new name (probably dependentRequired
, as it is a shorthand for conditional required
arrays).
I'd recommend focusing on the newer keywords for this sort of thing where possible.
It would be great if we could generate forms with conditionally required and conditionally present fields.
Property dependencies examples
In the following JSON Schema the form contains several fields and if the user enters anything into the
"credit_card"
field then the"billing_address"
field becomes required, otherwise it remains optional.Bidirectional dependencies could also be used; here the same fields are displayed but if the user enters anything into either
"credit_card"
or"billing_address"
then they both become required.Schema dependencies examples
In the following example the form initially only has the
"name"
(required) and"credit_card"
(optional) fields but if the user enters something into the"credit_card"
field then a new field is added to the form:"billing_address"
(required).The above examples were taken from Dependencies — object — Understanding JSON Schema 1.0 documentation.
Here is a more complex example of my own making: Here the potential power of dependencies really shines as the user can add multiple persons to the form, for each person they add their name and are then asked a single question and depending on the answer to that question an additional question may be asked specific to the specified answer for the specific person.
I would find being able to define a dynamic form using dependencies extremely beneficial as complex forms would be able to succinctly define in declarative JSON Schema their forms and perform server-side validation for submitted data using the same schemas.
Note; There is a lot here. I hope it isn't too overwhelming. I would suggest starting small by adding property dependencies support only and then slowly adding additional support for simpler to more complex schema dependencies.