Skip to main content
Version: Current (v7.0.0-beta.3)

A then or else list beside a field's own oneOf or anyOf is merged with it

Detect​

  • In your schema: a field with a oneOf or anyOf of its own and an if whose then or else holds a list under the same keyword, without a customMergeAllOf: an if on the field, or one on the parent object declaring the field's list, as then: { properties: { p: { oneOf: [C, D] } } }.
  • In your schema: a parent's dependencies schema declaring such a field's list, or the option of its own anyOf or oneOf the data picks, and a parent's branch declaring the list of a pattern or additional property, or of an array's items or additionalItems or a position of its tuple items or prefixItems, as then: { properties: { arr: { items: { oneOf: [C, D] } } } }.
  • In your uiSchema: oneOf/anyOf entries for such a field, written for the branch's options.
  • In your code or tests: retrieveSchema() results for such a field, or assertions on the options or labels its selector shows.
  • On the rendered page: the selector offers more options than before, labelled after two options each, as "A, C".

Change​

Before, the list of the then or else that applies replaced the field's own: retrieveSchema() returned the branch's options, Form offered them alone, and uiSchema.oneOf[i] described the branch's option at i.

Now the data has to match both lists, so the field offers each of its own options merged with each of the branch's, its own options first. Each pair keeps both options' constraints and takes the branch option's title, description, default and other annotations. Only those written on a branch's option that adds constraints to the field's option win: where it names a definition by $ref, the definition's annotations, and those of the options of a list the definition holds, give way to the field's option's. A $ref option repeating the field's option but for its annotations relabels it by its definition's annotations, unless the field's option is written as the same $ref, whose annotations written beside it stay. A pair whose options name different $refs or consts, or types no value meets both of, is left out (#5930, #5953). uiSchema.oneOf[i] describes the field's own option at i, which each pair built from it reads, and each pair is labelled after both options, as "A, C".

A field a parent's allOf entry declares is conjoined with the branch's declaration after the branch is merged, as the parent's allOf is merged into it after its branches. So a list only the entry declares comes second, as "C, A", and a branch closing the field with additionalProperties: false closes it, so the properties the entry adds are left out. An option the branch adds to a list only the entry declares is left out where it differs from the entry's options only in its annotations, as a default, since the entry's list read after it takes it for an option the branch left out.

An option both lists hold, equal but for its annotations, is offered alone, so a branch narrowing the field's list to some of its options, or relabelling them, offers just those. An option that only names a definition by $ref is read as that definition, so one written inline beside it in the other list repeats it. See An option both lists hold is offered alone.

const schema: RJSFSchema = {
type: 'object',
oneOf: [
{ title: 'A', properties: { a: { type: 'string' } } },
{ title: 'B', properties: { b: { type: 'string' } } },
],
if: { properties: { x: { const: 1 } } },
then: {
oneOf: [
{ title: 'C', properties: { c: { type: 'string' } } },
{ title: 'D', properties: { d: { type: 'number' } } },
],
},
};
createSchemaUtils(context, schema).retrieveSchema(schema, { x: 1 }).oneOf.map((option) => option.title);
// Before: ['C', 'D']. After: ['C', 'D', 'C', 'D'], A×C, A×D, B×C and B×D, which Form labels 'A, C', 'A, D', 'B, C' and 'B, D'

Write the uiSchema.oneOf/anyOf entries for the field's own options, and give a branch option's label as its title:

const uiSchema: UiSchema = {
oneOf: [
- { 'ui:title': 'uC' },
- { 'ui:title': 'uD' },
+ { 'ui:title': 'uA' },
+ { 'ui:title': 'uB' },
],
};

To keep offering the branch's options alone, move the field's own list into the else, so only one list applies at a time:

const schema: RJSFSchema = {
type: 'object',
- oneOf: [A, B],
if: { properties: { x: { const: 1 } } },
then: { oneOf: [C, D] },
+ else: { oneOf: [A, B] },
};

For an if on the parent object, move properties.p.oneOf into the parent's else the same way, as else: { properties: { p: { oneOf: [A, B] } } }. A parent's dependencies: { x: S } has no else, so write it as if: { required: ['x'] }, then: S and do the same.

See oneOf, anyOf, and allOf for how merged options are built and labelled.

Verify​

  • The selector offers the options you expect, labelled from your uiSchema entries and the branch options' titles.

Also read: