How to validate fields or whole entity when creating new entity programmatically

So if you are using entity_create to create new entity programmatically you may also want to have some validations, lets say you implemented that some field is unique and you don't want it to have more then one of the same value and you set that up with   ->addConstraint('UniqueField')

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

if you use UI for adding new entity, this will be applied and validation will run, but if you run 
 

 $crypto = entity_create('crypto', array(
      'name' => "Dai",
      'status' => '1',
    ));

$crypto->save();

you could run this to infinity, no validation would be applied. So to make that work you should do this

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

if ($violations->count() > 0) {
  // Validation failed.
}

so here you can check particular field or whole entity, with this you will get only one entity with particular "name" value.