Change record status: 
Project: 
Introduced in branch: 
main
Introduced in version: 
12.1.0
Description: 

Summary

"Required" in typed data meant three things: a value must be given, it may not be NULL, and a list or map may not be empty. They are now separate, as in JSON Schema.

Question Typed Data API JSON Schema Config schema
Must a map property be present? setRequired() required requiredKey: false makes a key optional. Default: required.
May the value be NULL? setNullable(). Default: nullable when not required. "null" in type nullable: true. Default: not nullable.
Is an empty list the same as NULL? ListDataDefinition::setNullMode(). NullMode::Loose, the default: the same. NullMode::Strict: not the same. minItems Never the same: an empty sequence or mapping is not NULL.

The config schema column is unchanged, and applies to config types that have the FullyValidatable constraint. It is there for comparison: config schema already answered the three questions separately.

A map property now validates as in JSON Schema, as long as the map has at least one property with a value other than NULL:

The property is Key left out Given as NULL
required, not nullable (the default for required) rejected rejected
required, nullable rejected allowed
optional, nullable (the default for optional) allowed allowed
optional, not nullable allowed rejected

If the map has no such property, as in {}, every cell is "allowed": validation does not visit the properties of an empty map, nor the items of an empty list, until #3256536.

// Must be present, may be NULL.
DataDefinition::create('string')->setRequired(TRUE)->setNullable(TRUE);

// A required list for which [] is valid. Add a Count constraint to require an item.
ListDataDefinition::create('string')->setRequired(TRUE)->setNullMode(NullMode::Strict);

To read the same from data: TypedDataInterface::isNull() (FALSE for '' and []) and Map::isAbsent().

Entity fields are always Loose. A map cannot be Strict yet: that waits for #3256536. On a field definition, setNullable() and setNullMode(NullMode::Strict) throw a \LogicException.

What you must change

Nothing, unless your code does one of these:

  1. It implements DataDefinitionInterface or TypedDataInterface without extending core's base class: add isNullable(): bool and getNullMode(): NullMode, or isNull(): bool.
  2. A subclass already has a member with a name that core now uses: isNullable, setNullable, getNullMode, setNullMode, isNull, isAbsent or $hasValue: match core's signature, or rename it.
  3. A test mocks DataDefinitionInterface: the mock returns FALSE from isNullable() unless the test configures it, so core adds the NotNull constraint for it.

Behavior changes for existing definitions

  1. NULL data with both NotNull and NotBlank gets one violation, not two.
  2. Reading a list property of a map that was set to NULL with setValue(NULL) no longer changes the map. Before, $map->get('tags') turned its value into ['tags' => []], and a required map then passed validation. For a map that was created without a value, the read still changes its value, until #3256536.
Impacts: 
Module developers