I have this schema
that is used by jsonforms.
const schema = {
$schema: 'http://json-schema.org/draft-07/schema#',
type: 'object',
properties: {
other_ubos: {
type: 'boolean',
},
persons: {
type: 'array',
items: {
type: 'object',
properties: {
name: {
type: 'string',
pattern: "^[-'a-zA-ZÀ-ÖØ-öø-ÿ ]+$",
},
function: {
type: 'string',
pattern: "^[-'a-zA-ZÀ-ÖØ-öø-ÿ ]+$",
},
email: {
type: 'string',
pattern: '^(.+)@(.+)$',
},
},
},
},
},
required: ['other_ubos'],
if: {
properties: {
other_ubos: {
const: true,
},
},
},
then: {
required: ['persons'],
},
};
I want to set a condition where if other_ubos
is true
each array item should have name``function
and email
required.
so basically something like this
const schema = {
$schema: 'http://json-schema.org/draft-07/schema#',
type: 'object',
properties: {
other_ubos: {
type: 'boolean',
},
persons: {
type: 'array',
items: {
type: 'object',
properties: {
name: {
type: 'string',
pattern: "^[-'a-zA-ZÀ-ÖØ-öø-ÿ ]+$",
},
function: {
type: 'string',
pattern: "^[-'a-zA-ZÀ-ÖØ-öø-ÿ ]+$",
},
email: {
type: 'string',
pattern: '^(.+)@(.+)$',
},
},
},
},
},
required: ['other_ubos'],
if: {
properties: {
other_ubos: {
const: true,
},
},
},
then: {
required: ['persons[number].name'],
},
};
setting required directly on
{
type: 'object',
properties: {
name: {
type: 'string',
pattern: "^[-'a-zA-ZÀ-ÖØ-öø-ÿ ]+$",
},
function: {
type: 'string',
pattern: "^[-'a-zA-ZÀ-ÖØ-öø-ÿ ]+$",
},
email: {
type: 'string',
pattern: '^(.+)@(.+)$',
},
},
}
won’t have the desired effect since it will validate even if other_ubos
is false
2
Answers
What i ended up doing is conditionally setting the whole properties object:
Or an even better solution:
You can partially repeat yourself within JSON Schema. It doesn’t really matter as the end validation is just a union of everything specified.
So a JSON Schema might look like this: