#2976356: Add a validation constraint to check if an entity has a field introduces a new validation constraint called EntityHasField. This constraint can be used on any validate-able entity (or, for example, a context which wraps around an entity), and will validate that the entity has a specific field available. It does not validate that the field actually has any value(s) in it -- just that the field is available at all (i.e., $entity->get($field_name) will return a FieldItemListInterface object).
If the entity is not fieldable (i.e., a config entity), validation will fail.
Note that this constraint does not verify the field type. For example: let's say you have a text field on nodes called field_fubar, and an image field on users called field_fubar. These are two completely different field types, but they have the same name, so both will pass the EntityHasField constraint because it only checks that a field with the given name is present. Therefore, if using this constraint to verify the presence of a field created by code (for example, hook_entity_base_field_info()), it is a good idea to prefix the name of the field with the name of your module. For example: my_module__special_base_field.
A simple example, using a context:
<?php
use \Drupal\Core\Plugin\Context\ContextDefinition;
use \Drupal\node\Entity\Node;
$definition = ContextDefinition::create('entity:node')->addConstraint('EntityHasField', 'field_example');
$node = Node::create(['type' => 'type_with_field_example']);
$context = new Context($definition, $node);
// $violations will have a size of 0, because the node has field_example, even though no value was set.
$violations = $context->validate();