/** * Strict request validation * * @param RequestInterface $request * * @throws ViolationListException */ private function strictRequestValidate(RequestInterface $request) { $requestMetadata = $this->validator->getMetadataFor($request); $useStrictGroup = false; // Search constraints by group "Strict" if ($requestMetadata instanceof ClassMetadata) { foreach ($requestMetadata->getConstrainedProperties() as $propertyName) { $propertyMetadata = $requestMetadata->getPropertyMetadata($propertyName); if (count($propertyMetadata)) { $propertyMetadata = array_shift($propertyMetadata); } else { continue; } $constraintsInStrictGroup = $propertyMetadata->findConstraints('Strict'); if (count($constraintsInStrictGroup)) { $useStrictGroup = true; break; } } } if ($useStrictGroup) { // The any properties in request class have a "Strict" group validation $violationList = $this->validator->validate($request, null, ['Strict']); if (count($violationList)) { throw ViolationListException::create($violationList); } return; } if ($this->validator instanceof VarTagValidatorInterface) { $violationList = $this->validator->validateObjectByVarTags($request); if (count($violationList)) { throw ViolationListException::create($violationList); } } }
/** * Create violation error response * * @param ViolationListException $exception * * @return JsonResponse */ public function createViolationErrorResponse(ViolationListException $exception) { $errorData = []; /** @var \Symfony\Component\Validator\ConstraintViolationInterface $violation */ foreach ($exception->getViolationList() as $violation) { $errorData[$violation->getPropertyPath()] = $violation->getMessage(); } // Try get code from errors storage via exception $errors = $this->handler->getErrors(); $code = $errors->hasException($exception) ? $errors->getExceptionCode($exception) : 0; return $this->createErrorResponse($code, null, $errorData); }