I do not understand how the Boolean (just for example) is validated. This is my code:

$tdm = \Drupal::typedData();
$data = $tdm->create(array('type' => 'boolean'), 1000);

The $data->validate() returns a list of ConstraintViolations, but where is the logic of these constraints, per Drupal\Core\TypedData\Plugin\DataType\Boolean does not implements getConstraints method.


1 Answer 1


Primitive types are special case.

By default, they get the PrimitiveType validation constraint in TypedDataManager::getConstraints().

// Auto-generate a constraint for data types implementing a primitive
// interface.
if (is_subclass_of($type_definition['class'], '\Drupal\Core\TypedData\PrimitiveInterface')) {
  $constraints[] = $validation_manager->create('PrimitiveType', array());

Validation constraints are usually provided in one of the following places:

  • The plugin annotation (see e.g. EntityReferenceItem)
  • base field/property definitions (see e.g. Node::baseFieldDefinitions(), title field)
  • By overriding the getConstraints() method on a typed data class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.