How to add constrain validation on entity_create So in my BaseFieldDefinition I have this below

 $fields['name'] = BaseFieldDefinition::create('string')
  -&gt;setLabel(t('Name'))
  -&gt;setDescription(t('The name of the Crypto entity.'))
  -&gt;addConstraint('UniqueField')


And I was hoping this would make this "name" field unique when creating new enteties. It does when I try to do that in UI, but when programmatically making new entities it is just ignored.

 $crypto = entity_create('crypto', array(
  'name' =&gt; "Dai",
  'status' =&gt; '1',
));
$crypto-&gt;save();


So should I use some other method for creating entities or do I need to add some manual validation here?

Also extra question, how do I find list of "addConstraint" values that are present in core?
Entities aren't validated automatically when saved via the API. (I'd be interested in knowing more about why not.) See the validation API docs for examples of validating a field and a whole entity:

$violations = $entity-&gt;field_text-&gt;validate(); // single field
$violations = $entity-&gt;validate();             // whole entity

if ($violations-&gt;count() &gt; 0) {
  // Validation failed.
}


To find all possible constraints, look for anything extending (possibly indirectly) \Symfony\Component\Validator\Constraint.