Summary
Validation constraints can contribute keywords to the JSON Schemas generated by Drupal's serializer. The primitive-data normalizer adds supported constraint metadata to the schema for its normalized output.
A new optional interface, Drupal\Core\Serialization\JsonSchemaConstraintInterface, exposes:
public function getJsonSchemaKeywords(): array;Core implements the interface for these constraints:
| Constraint | Schema keywords |
|---|---|
| Length | minLength, maxLength |
| Range | minimum, maximum |
| AllowedValues | enum for supported static choices |
anyOf containing an empty-string alternative and a pattern that requires one @ |
Before and after
For example, schema generation for this required string definition previously returned only ['type' => 'string']:
use Drupal\Core\TypedData\DataDefinition; $definition = DataDefinition::create('string') ->setRequired(TRUE) ->addConstraint('AllowedValues', ['choices' => ['USD', 'CAD']]) ->addConstraint('Length', ['max' => 3]); $data = \Drupal::typedDataManager()->create($definition); $schema = \Drupal::service('serializer')->getJsonSchema($data, []);
The resulting schema now includes:
[ 'type' => 'string', 'enum' => ['USD', 'CAD'], 'maxLength' => 3, ]
Contract for contributed and custom constraints
Constraints can implement the interface to opt in. Returned keywords must allow every value accepted by the constraint. Returning a less restrictive schema is permitted; return an empty array when no suitable keywords can be supplied.
Return enum choices in their original types. The schema helper casts them to the normalized type of the applicable branch and removes duplicates. Unsupported enum representations are omitted.
Subclasses of LengthConstraint, RangeConstraint, AllowedValuesConstraint, and EmailConstraint inherit getJsonSchemaKeywords(). If a subclass's validator accepts values that the parent constraint rejects, override the method and return only keywords that allow those values, or an empty array.
Supported options and scope
- Length: Exports non-negative integer limits for UTF-8 Unicode code-point counting without a normalization callback. Other counting modes and unsupported options supply no length keywords.
- Range: Exports finite integer or float bounds independently. Dynamic property-path bounds are omitted, while an available static bound can still be exported.
- AllowedValues: Exports non-empty static choices for a single value. Callback-based choices, multiple selections, and
match: FALSEsupply no enum keywords. - Email: Without a normalization callback, exports the following fragment in strict mode:
{ "anyOf": [ {"const": ""}, {"pattern": "^[^@]+@[^@]+$"} ] }
The helper reads configured and default constraints from DataDefinitionInterface::getConstraints() and instantiates them through the validation constraint manager. Nullable schemas retain their explicit null branch. Existing overlapping keywords are preserved with allOf.
The integration is in PrimitiveDataNormalizer. Normalizers that transform a value into a different representation require separate consideration. Normalized payloads are unchanged, but schema consumers can receive additional restrictions.
The Email data type's existing format: email attribute remains unchanged. Its compatibility with accepted empty and international values is tracked in #3643896.
Regex and Count mappings are tracked separately in #3643475 and #3643476.