How do I validate an entity before programmatically saving it? I have various functions/methods that programmatically create entities (nodes). I just noticed that invalid data has been getting stored in the db though these functions (invalid in that it doesn't meet the rules defined for that field).
How do I validate the data before $entity-&gt;save()?
I have seen documentation saying that I simply need to do $entity-&gt;validate() and then count if there are any errors; but I have marked a few fields as required and then left them empty in my API function and $entity-&gt;validate()-&gt;count() is always 0.
What am I missing?
Looking the api it should be simple:
$violations = $entity-&gt;validate();
  if ($violations-&gt;count() &gt; 0) {
    // Validation failed.
  }