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

A null option stores null

Detect​

  • In your schema: a property whose oneOf or anyOf offers an option of type: 'null' or enum: [null], the usual way to write a nullable field, or of const: null or type: ['null'] beside an own type that leaves null out.
  • In your code: what reads the submitted data, such as onSubmit or onChange, and expects no value for such a property.
  • In your schema: a dependencies, dependentRequired or dependentSchemas entry for such a property.
{
"type": "object",
"required": ["nickname"],
"properties": {
"nickname": {
"oneOf": [
{ "title": "Text", "type": "string" },
{ "title": "None", "type": "null" }
]
}
}
}

Change​

Picking the option set the property to undefined, so a required one couldn't be submitted. It now sets it to null, as an option written as const: null or type: ['null'] and an array item did (#5944). A property whose own type leaves null out, such as "type": "object" beside the options, and has no nullable: true, gets undefined however the option is written, where one written as const: null or type: ['null'] stored a null that failed validation.

A required property with no data that opens on such an option held nothing and failed validation until the user switched options and back. getDefaultFormState() and the form's starting data now hold null for it, unless that would add an optional object to the data. An optional property still starts absent.

Code that reads the data gets null where it got no value, and a dependencies, dependentRequired or dependentSchemas entry for the property now applies once the option is picked, as it does for any value. To send no value instead, remove it as you submit:

-onSubmit={({ formData }) => save(formData)}
+onSubmit={({ formData: { nickname, ...rest } }) => save(nickname === null ? rest : { ...rest, nickname })}

Verify​

  • Picking the null option submits null, and a required property with it passes validation.
  • A required property that opens on the null option starts at null.
  • The dependencies of the property apply once the option is picked.

Also read: