Problem/Motivation

#3627330 adds keywords from validation constraints to the JSON Schema of primitive data. It reads the constraints on the primitive's own data definition. Field types can also add constraints to their properties through a ComplexData constraint on the field item, and those are not included.

StringItem::getConstraints() adds Length this way for the max_length setting. The schema for the value property has no maxLength, so a schema consumer can send a string that Drupal then rejects.

Steps to reproduce

With #3627330 applied, the name field on entity_test has a max_length of 64:

$entity = EntityTest::create(['name' => 'abc']);
$value = $entity->get('name')->first()->get('value');
$schema = \Drupal::service('serializer')->getJsonSchema($value, []);

Actual: {"type": "string"}

Expected: {"type": "string", "maxLength": 64}

Proposed resolution

Read the ComplexData constraints from the field item's getConstraints(). For each property they list, add the keywords from constraints that implement JsonSchemaConstraintInterface to that property's schema.

Remaining tasks

  • Decide whether the keywords are added when the field item's schema is built, or also when the schema of a single property is requested. #3627318 adds the schema for complex data.
  • Write the implementation and tests.

User interface changes

None.

API changes

None.

Data model changes

None.

Comments

mglaman created an issue.