function JsonValidator::validate
Overrides ConstraintValidatorInterface::validate
File
-
vendor/
symfony/ validator/ Constraints/ JsonValidator.php, line 24
Class
- JsonValidator
- @author Imad ZAIRIG <imadzairig@gmail.com>
Namespace
Symfony\Component\Validator\ConstraintsCode
public function validate(mixed $value, Constraint $constraint) : void {
if (!$constraint instanceof Json) {
throw new UnexpectedTypeException($constraint, Json::class);
}
if (null === $value || '' === $value) {
return;
}
if (!\is_scalar($value) && !$value instanceof \Stringable) {
throw new UnexpectedValueException($value, 'string');
}
$value = (string) $value;
if (!json_validate($value)) {
$this->context
->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setCode(Json::INVALID_JSON_ERROR)
->addViolation();
}
}