Form keeps or drops a value by comparing the field trees before and after a change
Detect
- Your schema has
if/then/else,dependencies,allOf,anyOf/oneOfor$refthat change which fields render or what they accept as the data changes. - Your app changes the
schemaprop of a mounted form, ordefaultFormStateBehavior. - Tests that assert which values remain after such a change.
Change
Form sanitizes its data by comparing the field tree the data builds before a change with the one it builds after, field by field, as each is rendered: with the option the form selects and the $ref cycles the user expanded.
A field that left the tree loses its value, unless the object still holds it as an extra key that constrains nothing, as under additionalProperties: true, and that extra key renders it: a scalar, or an object whose keys the extra key takes as its own, stays and renders as an extra key. An array, whose items such an extra key renders none of, is dropped. So is a value equal to what the form fills the old field with from data without it, such as its default, its const, an empty object or its ui:initialValue or ui:emptyValue, even one the user chose.
A kept value is judged by the extra key alone, so a limit the new branch puts on the object's keys, such as propertyNames or maxProperties, can reject it, which validation reports. For the form to drop the value instead, give that branch additionalProperties: false, with the keys it keeps declared in its own properties.
A field whose constraints changed has its value judged again. The constraints are its type, const, enum or constant options, maxItems, and the default the defaults fill it with, from its own schema or any ancestor's default object as defaultFormStateBehavior reads them (see getFilledDefault()).
- A value is dropped for a changed
typeonly where the new one leaves out a type the old one named, anumbercounting as naminginteger, and can't hold the value, a property's as an item's (#5944). - A value still holding the old
defaulttakes the new one, written in place. An array holding it, or any value the olddefaultwas filled out from, tominItemsor through aui:initialValuebelow the field, is filled out from the new one in place, as a fresh mount fills it, since the form applies aui:initialValueonly on mount. A value thedefaultplays no part in, such as one the field's ownui:initialValuefilled in or one whose every item anitemsui:initialValuefilled, is kept. - A property declared
readOnlyholding a scalar or a select's value is removed, unless a newdefaultreplaces an old one. A value of another type, or one the newconstorenumrejects, is replaced or dropped assanitizeDataForNewSchema()does it. - A field the old schema gave no
defaultkeeps its value when adefaultfirst applies to it, areadOnlyone included, since nothing tells saved data apart from a value itsui:initialValueorui:emptyValuefilled in. - An object the defaults fill key by key, one whose own
typerenders asobjectand whose newdefaultis an object too, is judged a key at a time, against the objectdefaultit holds a key of. That excludes a select over whole objects and an object with atrueproperty. A key the newdefaultno longer names keeps its value. - An array holding an object is judged an item at a time too, since the old filled
defaultit equals as a whole can hold a value nodefaultreached, such as a required boolean'sfalse. Withlist: { type: 'array', default: [{ a: 'x' }], items: { type: 'object', required: ['f'], properties: { a: { type: 'string' }, f: { type: 'boolean' } } } }and the data{ list: [{ a: 'x', f: false }] },fgainingdefault: truekeepsf: false. Only an array holding the verydefaultits old schema declares is filled out from the new one whole, rows and all. - An array still holding its old filled
defaultas a whole follows the new one's rows:[{ a: 'x', f: false }, { a: 'w', f: false }]becomes[{ a: 'y', f: false }]where thedefault[{ a: 'x' }, { a: 'w' }]becomes[{ a: 'y' }], and[{ a: 'y', f: false }, { f: false }]underminItems: 2, the padding a fresh mount fills. A row the new items reject takes the newdefault's entry at its index, and a longer newdefaultadds its rows past the array's end:[{ a: 'y', f: false }]becomes[{ a: 'x', f: false }, { a: 'w', f: false }]the other way round. Once the user added or changed a row, every row stays. A multi-select's values are one choice, so none takes its array'sdefaultat its position. - An array's items are judged by the schema their position declares: a
prefixItemsor tupleitemsentry, and past themitems, oradditionalItemsbeside a tuple. A position the old schema declaredtrue,falseor nothing at judges the item by the new schema alone. - Where an item reaches a position the new schema closes with
false, or the new schema rejects a prefix item, whose removal would move the items after it, the whole array is removed instead, for the defaults to fill, assanitizeDataForNewSchema()clears it. The array keeps its items where the old schema closed that position withfalsetoo, and where both schemas declare a schema for a rejected prefix item's position and constrain it alike, itsdefaultaside, so the item already failed the old one. It also keeps them where the item is an unfilled prefix item or is at or past the newmaxItems, where the user is editing the array or a field in it, and where the array is an item of another array.
A field whose constraints did not change keeps its value, whatever else the change did.
Changing defaultFormStateBehavior alone changes no schema, but it can change the default a field is filled with, so every field is judged again: with default: { v: 'x' } on the root and v: { type: 'string', default: 'q' }, a form that filled v with 'q' gives { v: 'x' } once nestedDefaultsPrecedence becomes 'ancestorWins'.
No code has to change. Update tests; "before" is what 6.11.0 gives:
- A condition inside the selected option replaces its old default (#5465). With
cfgFor = (name) => ({ type: 'object', default: { name }, properties: { name: { type: 'string' } } }),thing: { oneOf: [X, Y] }whereXis{ type: 'object', properties: { mode: { enum: ['a', 'b'], default: 'a' } }, dependencies: { mode: { oneOf: [{ properties: { mode: { const: 'a' }, cfg: cfgFor('a') } }, { properties: { mode: { const: 'b' }, cfg: cfgFor('b') } }] } } }, and the data{ thing: { mode: 'a', cfg: { name: 'a' } } }, selectbformode. Before:cfg: { name: 'a' }. After:cfg: { name: 'b' }. - A default declared by a branch that requires the key is replaced too. With
cfgdeclared plainly andallOf: [{ if: { properties: { mode: { const: 'a' } }, required: ['mode', 'cfg'] }, then: { properties: { cfg: cfgFor('a') } } }, ...the same for 'b'], selectbformodewith the data{ mode: 'a', cfg: { name: 'a' } }. Before:cfg: { name: 'a' }. After:cfg: { name: 'b' }. - An array or object write is sanitized like any other edit. With
list: { type: 'array', items: { type: 'string' } },color: { enum: ['r', 'g'] }andallOf: [{ if: { properties: { list: { minItems: 2 } } }, then: { properties: { color: { enum: ['r'] } } } }], add a row tolistwith the data{ color: 'g', list: ['x'] }. Before:color: 'g'until the next edit replaced it. After:color: 'r'as the row is added. - A schema change of a form that owns its data is sanitized as it arrives, unless the form is
disabledorreadonly, whose data no edit sanitizes either. Withcolor: { $ref: '#/definitions/Color' },Colornarrowed fromenum: ['a', 'b']toenum: ['a'], andinitialFormData{ color: 'b' }. Before:color: 'b', kept until an edit sanitized and, with no conditional in the schema, for good. After:color: 'a'at once. Whether 6.11.0 ever replaced the value turned on a conditional elsewhere in the schema; the form now applies the rule an edit follows to the schema change itself. - A value the new
typeholds is kept (#5944). Withs: { type: 'string' },i: { type: 'integer' },mode: { type: 'string' }andif: { properties: { mode: { const: 'wide' } }, required: ['mode'] }, then: { properties: { s: { type: ['string', 'null'] }, i: { type: 'number' } } }, typewideintomodewith the data{ s: 'a', i: 3 }. Before:{ mode: 'wide' }, as on a switch betweenanyOf/oneOfoptions declaring those types. After:{ mode: 'wide', s: 'a', i: 3 }. To clear a field with the branch or the option, set it toundefinedintransformChange. - A value
setFieldValue('', data)sets is taken as written, and no edit judges it unless the edit changes that field's constraints. Withmode: { enum: ['basic', 'advanced'] },color: { type: 'string' },note: { type: 'string' }andallOf: [{ if: { properties: { mode: { const: 'advanced' } } }, then: { properties: { color: { enum: ['r', 'c'] } } }, else: { properties: { color: { enum: ['r'] } } } }], set{ mode: 'basic', color: 'c' }and type intonote:color: 'c'stays, which validation reports. An arraysetFieldValue()writes whole at its own path pairs each item that holds what an item of the array held with that item, so a reorder set back keeps the constraints its rows had. - An extra key holding the old branch's
additionalPropertiesdefault takes the new one, as a declared property does. Withmode: { enum: ['a', 'b'], default: 'a' }anddependencies: { mode: { oneOf: [{ properties: { mode: { const: 'a' } }, additionalProperties: tagFor('a') }, { properties: { mode: { const: 'b' } }, additionalProperties: tagFor('b') }] } }, wheretagFor = (tag) => ({ type: 'object', default: { tag }, properties: { tag: { type: 'string' } } }), and the data{ mode: 'a', extra: { tag: 'a' } }, selectbformode. Before: the form threwCannot use 'in' operator to search for 'tag' in aas it rendered. After:extra: { tag: 'b' }.
Switching an anyOf/oneOf option follows the same diff; see Switching an anyOf or oneOf option keeps the keys the new option takes and A switch between options keeps a scalar the new option holds. A string a new date or time format can't show is dropped; see A string is dropped where a new date or time format can't show it. Code that calls sanitizeDataForNewSchema() itself sees a different result shape; see sanitizeDataForNewSchema removes discarded keys and leaves out objects the data doesn't hold.
Verify
- Values for fields that left the form are gone from the submitted data, and values for fields whose constraints didn't change remain.
- A value you want dropped when a branch limits the object's keys is dropped once that branch has
additionalProperties: false.
Also read:
- Switching an anyOf or oneOf option keeps the keys the new option takes
- sanitizeDataForNewSchema removes discarded keys and leaves out objects the data doesn't hold
- An anyOf or oneOf field keeps the option the user selected
- ui:required and omitExtraData follow the anyOf or oneOf option on screen
- A switch between options keeps a scalar the new option holds
- A string is dropped where a new date or time format can't show it