A constant oneOf or anyOf starts from a default outside its constants
Detect
- In your schema: a field whose
oneOforanyOflists constants, with adefaultthat names none of them, its own or one its object'sdefaultgives it (anullincluded). - In your form data: no value for that field, or one the defaults replace: a
NaN, or anullat the root or undermergeDefaultsIntoFormData: 'useDefaultIfFormDataUndefined'.
Change
Such a field started from the first constant, and now starts from the default as it is, as an enum does, and as a value outside the constants in the form data is replaced by it (#5895). With this schema and no formData, getDefaultFormState() and the form's starting data were "red" and are now "blue":
{ "type": "string", "default": "blue", "oneOf": [{ "const": "red" }, { "const": "green" }] }
The same goes for a default of null, for a list an allOf brings in, and for a select over object constants. Under constAsDefaults: 'always' the first constant still wins, as it wins over a value in the form data. Under allOf: 'skipDefaults', a field with an allOf whose one constant sits in an option of its own anyOf, such as "anyOf": [{ "oneOf": [{ "const": 3 }] }], still starts from that constant.
A field whose type doesn't allow null takes an ancestor's null too, as an enum does, where it took its first constant. With this schema and no formData, the result was { "c": "red" } and is now { "c": null }, which fails validation as the enum field's does:
{
"type": "object",
"default": { "c": null },
"properties": { "c": { "type": "string", "oneOf": [{ "const": "red" }, { "const": "green" }] } }
}
To start from the first constant again, make it the default:
-{ "type": "string", "default": "blue", "oneOf": [{ "const": "red" }, { "const": "green" }] }
+{ "type": "string", "default": "red", "oneOf": [{ "const": "red" }, { "const": "green" }] }
For an ancestor's null, give the field a default of its own. It wins over the ancestor's whatever nestedDefaultsPrecedence says, since the field's type can't hold the null:
-"c": { "type": "string", "oneOf": [{ "const": "red" }, { "const": "green" }] }
+"c": { "type": "string", "default": "red", "oneOf": [{ "const": "red" }, { "const": "green" }] }
Verify
- The field starts from the value you expect when the form has no data for it.
- No field starts at a
nullitstyperejects.
Also read: