How to add constraints to custom data arrays?
You cannot validate generic data against a list of constraints you create.
Constraints are attached to specific typed data objects, entity fields, or entities. You can only validate them against the constraints they have, and you don't need to get first the list of the constraints that apply.
If $entity contains an entity, you can use the following code to check if it validates against its constraints.
$violations = $entity-&gt;validate();
if ($violations) {
  // The entity didn't pass validation from constraints.
  // For each validation $validation-&gt;getMessage() returns the
  // error message. $violation-&gt;getPropertyPath() returns the property
  // path for the property that failed validation, for example
  // field_text.0.value, if the validation failed for the value property of
  // field_text.
}

To validate only a single entity field, for example field_text in $entity, instead of $entity-&gt;validate(), which would validate the entity and all its entity fields, the method to call is $entity-&gt;field_text-&gt;validate().