Problem/Motivation
A data definition describes exactly one data type. A value that takes one of several shapes falls back to the any type, which has no validation and no schema. The json_schema serialization format has no source-side representation for oneOf, so schemas for union-shaped data, such as component props, cannot be produced from typed data.
Steps to reproduce
N/A
Proposed resolution
Add a union data type. Each choice reuses an existing typed data convention.
UnionDataDefinitionholds an ordered list of member data definitions, composed the way list item and map property definitions are. Members may be any definition, including maps, lists and unions.- The
Uniontyped data class stores the raw value;setValue()never rejects. Resolution runs lazily on first access and is memoized until the nextsetValue(). Traversal is delegated to the resolved member's typed data.
Resolution order:
- NULL resolves to no member.
isRequired()governs nullability. - A resolver declared on the definition, a static method string like
default_value_callback, returns a member or falls through. - Structural match on the members' primitive or complex interfaces, exact PHP type, no coercion, in declaration order. A single candidate resolves with no validation run.
- Remaining candidates are probed by validation in declaration order. The first member that validates wraps the value, and the probe instance becomes the delegate. Zero structural candidates probe every member.
- No match leaves the value set and unresolved. Validation reports one union violation plus the best candidate's violations: fewest violations, declaration order breaks ties.
- Validity is order-independent. Resolution precedence follows declaration order.
- Membership is a constraint plugin registered as a default constraint. Strict exactly-one matching is an option on it and affects validation only, never resolution. Union-level constraints run against the raw value.
- The schema normalizer appends a
nullbranch tooneOffor optional data, matchingPrimitiveDataNormalizer. - Mutations through the resolved member do not re-resolve.
$link = UnionDataDefinition::create( DataDefinition::create('uri'), MapDataDefinition::create() ->setPropertyDefinition('uri', DataDefinition::create('uri')) ->setPropertyDefinition('title', DataDefinition::create('string')), );
This depends on #3616328: the removed cache key collided for members declaring the same property names. JSON Schema emission lands in a follow-up serialization issue, matching the pattern of #3623431.
Remaining tasks
Agree on the resolution and validation semantics. Patch with unit and kernel tests. File the serialization follow-up. Discriminators, config schema, Field API properties and schema import are out of scope.
User interface changes
None
API changes
New: a union data type plugin, UnionDataDefinition and its interface, and a membership constraint. Additive only.
Data model changes
None
- [x] AI Assisted Issue: This issue was generated with AI assistance, but was reviewed and refined by the creator.
- [x] AI Assisted Code: This code was mainly generated by a human, with AI autocompleting or parts AI generated, but under full human supervision.
Issue fork drupal-3624067
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
michaellander commentedAn example where this would be useful is if you wanted to accept an
fidoruriin reference to a file, and still make those individually validatable.Comment #3
michaellander commentedComment #6
michaellander commentedComment #7
michaellander commented