A tuple in a then or else is met with the field's tuple position by position
Detect
- In your schema: an array field with a tuple,
itemsas a list orprefixItems, and athen,elseordependenciesschema declaring a shorter tuple for it under the same keyword, on the field or on a parent object, asthen: { properties: { p: { items: [{ title: 'Short name' }] } } }. - In your schema: such a field with
additionalItems, oritemsbesideprefixItems, whose branch declares a longer tuple than the field's own, on the field or on a parent object, asthen: { items: [{}, { title: 'Width' }] }for a field with one position. - In your schema: an array field with a tuple whose
additionalItems, oritemsbesideprefixItems, a branch declares too, with atrueorfalseon either side, asthen: { additionalItems: { minimum: 5 } }for a field declaringadditionalItems: false; the same withunevaluatedItemswhere the branch is on a parent object; a branch declaringadditionalItems: falsewith noitemsbeside it; a branch tuple with afalseposition; a field whoseitems, one schema or a list, a branch declares astrue; or a field declaringitems: falsewhose branch declaresitemstoo. - In your code or tests:
retrieveSchema()results for any array field whose branch declares a tuple under the same keyword, whatever its length, since each position now keeps the field's own constraints;getDefaultFormState()results holding the default of a field the bullets above describe; or assertions on the rows such a field renders. - In your uiSchema: a
uiSchema.additionalItemsentry on such a field whose branch also declaresadditionalItems. - On the rendered page: the field renders its own positions past a shorter branch tuple, where it rendered only the branch's, and renders a position only a longer branch tuple declares with the type of the field's
additionalItems.
Change
Before, the branch's tuple replaced the field's own, so the form rendered and filled only the branch's positions, though the validator accepted items at the field's positions past them. Now the two tuples are met position by position. Each position keeps the constraints of both, and a position only the field declares also takes the branch's additionalItems, or its items beside prefixItems, where the branch declares one, or else its unevaluatedItems. A position only a branch declares likewise takes the field's additionalItems, or its items beside prefixItems, whether the branch is on the field or on a parent, so it renders with that schema's type and is filled with its default. Where the field declares neither, the position takes the field's unevaluatedItems where only a parent's branch declares it, as then: { properties: { p: … } }, and not where a branch on the field itself does, alone or beside a parent's.
With this schema and the form data { mode: 'short' }, p rendered one row, Short name, and getDefaultFormState() filled p: [undefined]. It now renders Short name and Age, and fills p: [undefined, undefined]:
import { RJSFSchema } from '@rjsf/utils';
const schema: RJSFSchema = {
type: 'object',
properties: {
mode: { type: 'string', enum: ['short', 'long'] },
p: { type: 'array', items: [{ type: 'string', title: 'Name' }, { type: 'number', title: 'Age' }] },
},
if: { properties: { mode: { const: 'short' } }, required: ['mode'] },
then: { properties: { p: { items: [{ type: 'string', title: 'Short name' }] } } },
};
The items past the tuple are met the same way. Before, where either side declared true or false for them under additionalItems, or items beside prefixItems, what the branch declared replaced what the field declared. Now they take both. A false on either side allows no item past the tuple, so a branch's additionalItems, or items beside prefixItems, can't reopen a tuple the field closes with the same keyword, and a true leaves the other side's schema in place. A branch's additionalItems: false with no items beside it is the exception: a validator ignores it there, so it no longer closes the field's tuple, and the form offers to add the items the field's own additionalItems describes. A field's items and a branch's are met alike where either is true or false, so a branch's items: true leaves the field's item schema or tuple in place. The same holds for unevaluatedItems where the branch is on a parent object, as then: { properties: { p: … } }. A false at a position of the met tuple ends the tuple there, and the field is closed past it. With this schema the form offered to add items, which the validator rejected. It now offers none:
import { RJSFSchema } from '@rjsf/utils';
const schema: RJSFSchema = {
type: 'array',
items: [{ type: 'string' }],
additionalItems: false,
if: true,
then: { additionalItems: { minimum: 5 } },
};
To keep a shorter tuple, close the branch's tuple with additionalItems: false, or with items: false or unevaluatedItems: false beside prefixItems. The validator then rejects an item past it as well:
- then: { properties: { p: { items: [{ type: 'string', title: 'Short name' }] } } },
+ then: { properties: { p: { items: [{ type: 'string', title: 'Short name' }], additionalItems: false } } },
A branch that also declares additionalItems can't be closed this way. The rows past its tuple were additional items and are now positions of the field's tuple, so they read uiSchema.items at their index where they read uiSchema.additionalItems, and they no longer offer Remove. Move the entry to each of those positions:
import { RJSFSchema } from '@rjsf/utils';
const schema: RJSFSchema = {
type: 'array',
items: [
{ type: 'string', title: 'Unit', enum: ['size', 'text'] },
{ type: 'string', title: 'First' },
{ type: 'string', title: 'Second' },
],
if: { items: [{ const: 'size' }] },
then: { items: [{}], additionalItems: { enum: ['S', 'M', 'L'] } },
};
-const uiSchema = { additionalItems: { 'ui:widget': 'radio' } };
+const uiSchema = { items: [{}, { 'ui:widget': 'radio' }, { 'ui:widget': 'radio' }] };
A branch's longer tuple changes the form too. With this schema, the branch's second position was retrieved as { title: 'Width' } and getDefaultFormState() filled [undefined, undefined]. It is now retrieved as { type: 'number', default: 3, title: 'Width' } and renders as a number field, and getDefaultFormState() fills [undefined, 3]. The validator already rejected anything but a number there:
import { RJSFSchema } from '@rjsf/utils';
const schema: RJSFSchema = {
type: 'array',
items: [{ type: 'string', title: 'Label' }],
additionalItems: { type: 'number', default: 3 },
if: true,
then: { items: [{}, { title: 'Width' }] },
};
Verify
- The field renders the rows you expect under each branch, and
getDefaultFormState()fills that many items. - Each row keeps the constraints the field declares for its position beside those the branch adds.
- A row only a longer branch tuple declares renders with the type of the field's
additionalItems, and a form with no data for it fills that schema'sdefault. - A field whose tuple either side closes offers no Add button, and an added item keeps the constraints the field declares for the items past its tuple.
- The rows past the branch's tuple take the uiSchema you set for their positions, and offer no Remove button.
Also read: