Chris@18: validate(); Chris@18: Chris@18: // Remove violations of inaccessible fields as they cannot stem from our Chris@18: // changes. Chris@18: $violations->filterByFieldAccess(); Chris@18: Chris@18: // Filter violations based on the given fields. Chris@18: if ($field_names !== NULL) { Chris@18: $violations->filterByFields( Chris@18: array_diff(array_keys($entity->getFieldDefinitions()), $field_names) Chris@18: ); Chris@18: } Chris@18: Chris@18: if (count($violations) > 0) { Chris@18: // Instead of returning a generic 400 response we use the more specific Chris@18: // 422 Unprocessable Entity code from RFC 4918. That way clients can Chris@18: // distinguish between general syntax errors in bad serializations (code Chris@18: // 400) and semantic errors in well-formed requests (code 422). Chris@18: // @see \Drupal\jsonapi\Normalizer\UnprocessableHttpEntityExceptionNormalizer Chris@18: $exception = new UnprocessableHttpEntityException(); Chris@18: $exception->setViolations($violations); Chris@18: throw $exception; Chris@18: } Chris@18: } Chris@18: Chris@18: }