"xor" validation with conditional schema rules

Hi i have a case where i mapped my json-schema with two refs properties

“contactType”: {
“type”: “string”,
“enum”: [“person”, “company”]
},
“person”: {
“$ref”: “#/definitions/person”
},
“company”: {
“$ref”: “#/definitions/company”
}

except the contactType each of them is a complex structure and both of them have at least one required field.
I have made a UI schema with conditional rules that maps with success my use-case to show each group when the relative radio button is clicked … but i have an unexpected behavior with validation.

The required fields are only on nested fields and the validator works well with them when i visit at least one field of the selected group… what i need is that the entire group (person or company) should become required when the contactType was chosen.

So i have a XOR validation to apply on person OR contact but i never need to validate “contact” AND “person” together.

Is there a way to apply a conditional require on a group of fields? In my case the condition should be:

contactType === ‘person’ => [required: #/properties/person]
contactType === ‘company’ => [required: #/properties/company]

Hi!

This is more of a JSON Schema question. In JSON Schema you can not set a require on a group of fields, it must be specified for each field separately.

Using if/then/else and combinators like oneOf/allOf/anyOf it’s possible to express conditional and complex cases.

Note that while the require validation will work in JSON Forms, we will not show a “require” star on conditionally required fields.

Hi @sdirix and thank you so much for your reply.
After a set of considerations about this topic we found more simple at this poin to manage flat properties in the json-schema and map a group of elements in a custom layout from the ui-schema. In this way we can apply visibility RULES on field level of each group or apply a visibility RULE level on the entire group.

In this way for the conditional validation using this approach solved my issue:

“if”: { “properties”: { “contactType”: { “enum”: [“person”] } } },
“then”: {
“required”: [“contactType”,“surname”]
},
“else”: {
“if”: { “properties”: { “contactType”: { “enum”: [“company”] } } },
“then”: {
“required”: [“contactType”,“companyName”]
}
}

Thanks for your help that is really appreciated

1 Like