Checks if the passed value is valid.

@param DataInterface $data       The value that should be validated
@param Constraint    $constraint The constraint for the validation

@throws Exception
770075
public function validate($data, Constraint $constraint)
    {
        if (!$data instanceof $this->dataClass) {
            $class = \get_class($data);
            throw new \UnexpectedValueException("Can't validate data of class {$class}");
        }
        $context = $this->context; // VERY IMPORTANT ! context will be lost otherwise
        foreach ($data->getFamily()->getAttributes() as $attribute) {
            if ($attribute->isRequired() && $data->isEmpty($attribute)) {
                $this->buildAttributeViolation(
                    $data,
                    $context,
                    $attribute,
                    'required',
                    $data->get($attribute->getCode())
                );
            }
            if ($attribute->isUnique()) {
                $this->checkUnique($context, $attribute, $data);
            }

            if ($attribute->getOption('allowed_families')) {
                $this->validateFamilies($context, $attribute, $data);
            }

            if (\count($attribute->getValidationRules())) {
                $this->validateRules($context, $attribute, $data);
            }
        }
    }
1058258
public function validate($value, Constraint $constraint)
    {
        if ($value === null) {
            return;
        }

        if (!$constraint instanceof Range) {
            throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Range');
        }

        if ($value instanceof \DateTime || $value instanceof \DateTimeInterface) {
            $newValue = clone $value;
            $newValue->setTime(0, 0, 0);
            $value = $newValue;
        }

        if (is_array($constraint->min)) {
            if (!(array_key_exists('service', $constraint->min) && array_key_exists('method', $constraint->min))) {
                throw new ConstraintDefinitionException(sprintf('The value of the option "min" as a factory expects the keys "service" and "method".'));
            }

            $minFactory = array($this->container->get($constraint->min['service']), $constraint->min['method']);
            if (!is_callable($minFactory)) {
                throw new ConstraintDefinitionException(sprintf('"%s::%s" targeted by Range constraint is not a valid callable.', get_class($minFactory[0]), $minFactory[1]));
            }

            $min = call_user_func($minFactory);
            if ($min instanceof \DateTime || $min instanceof \DateTimeInterface) {
                $constraint->min = clone $min;
            } else {
                $constraint->min = $min;
            }
        }

        if (is_array($constraint->max)) {
            if (!(array_key_exists('service', $constraint->max) && array_key_exists('method', $constraint->max))) {
                throw new ConstraintDefinitionException(sprintf('The value of the option "max" as a factory expects the keys "service" and "method".'));
            }

            $maxFactory = array($this->container->get($constraint->max['service']), $constraint->max['method']);
            if (!is_callable($maxFactory)) {
                throw new ConstraintDefinitionException(sprintf('"%s::%s" targeted by Range constraint is not a valid callable.', get_class($maxFactory[0]), $maxFactory[1]));
            }

            $max = call_user_func($maxFactory);
            if ($max instanceof \DateTime || $max instanceof \DateTimeInterface) {
                $constraint->max = clone $max;
            } else {
                $constraint->max = $max;
            }
        }

        return parent::validate($value, $constraint);
    }
339935
public function validate($addCartItemCommand, Constraint $constraint): void
    {
        Assert::isInstanceOf($addCartItemCommand, AddToCartCommandInterface::class);
        Assert::isInstanceOf($constraint, CartItemAvailability::class);

        /** @var OrderItemInterface $cartItem */
        $cartItem = $addCartItemCommand->getCartItem();

        $isStockSufficient = $this->availabilityChecker->isStockSufficient(
            $cartItem->getVariant(),
            $cartItem->getQuantity() + $this->getExistingCartItemQuantityFromCart($addCartItemCommand->getCart(), $cartItem)
        );

        if (!$isStockSufficient) {
            $this->context->addViolation(
                $constraint->message,
                ['%itemName%' => $cartItem->getVariant()->getInventoryName()]
            );
        }
    }
637065
public function addConstraint(Constraint $constraint) : void
    {
        $constraint->setValidator($this);
        array_push($this->constraints, $constraint);
    }
495318
public function validate($protocol, Constraint $constraint) {


        if (is_null($constraint->requireHash)) {
            $hashReg = '#{0,1}';
        } else if ($constraint->requireHash === true) {
            $hashReg = '#';
        } else if ($constraint->requireHash === false) {
            $hashReg = '';
        } else {
            throw new \Exception('The requireHash option value is NOT valid');
        }

        if (!$this->checkHexColor($protocol, $hashReg)) {
            $this->context->buildViolation($constraint->message)
                    ->setParameter('%color%', $protocol)
                    ->addViolation();
        }
    }
412022
public function validate($value, Constraint $constraint)
    {
        if (!$value instanceof SectionUpdateData) {
            return;
        }

        try {
            $section = $this->sectionService->loadSectionByIdentifier($value->identifier);
            if ($section->id == $value->getId()) {
                return;
            }

            $this->context->buildViolation($constraint->message)
                ->atPath('identifier')
                ->setParameter('%identifier%', $value->identifier)
                ->addViolation();
        } catch (NotFoundException $e) {
            // Do nothing
        }
    }
681280
public function validate($value, Constraint $constraint)
    {
        if (!$constraint instanceof Json) {
            throw new UnexpectedTypeException($constraint, Json::class);
        }

        if (null === $value || '' === $value) {
            return;
        }

        if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
            throw new UnexpectedTypeException($value, 'string');
        }

        if (json_decode($value) || json_last_error() === JSON_ERROR_NONE) {
            return;
        }

        $this->context->buildViolation($constraint->message)
            ->setParameter('{{ value }}', $this->formatValue($value))
            ->addViolation();
    }
1330059
public function validate($value, Constraint $constraint): void
    {
        if ($value !== null) {
            $type = $constraint->type;
            $values = $type::getValues();
            if (!\in_array($value, $values, true)) {
                $this->context->buildViolation($constraint->message)
                    ->setParameter('{{ type }}', $constraint->type)
                    ->setParameter('{{ value }}', $value)
                    ->addViolation();
            }
        }
    }
1084864
protected function createIssue(ConstraintInterface $constraint)
    {
        list($value, $pointer) = $this->currentContext();

        return new Result\ValidationIssue(
            $constraint,
            $value,
            $pointer
        );
    }
511527
public function validate($entity, Constraint $constraint): void
    {
        /** @var UniqueEntity $constraint */
        $em = Util::getObjectManager($this->registry, $entity, $constraint);
        $fields = (array) $constraint->fields;
        $criteria = $this->getCriteria($entity, $constraint, $em);

        if (null === $criteria) {
            return;
        }

        $result = $this->getResult($entity, $constraint, $criteria, $em);

        if (!$this->isValidResult($result, $entity)) {
            $errorPath = null !== $constraint->errorPath ? $constraint->errorPath : $fields[0];
            $invalidValue = $criteria[$errorPath] ?? $criteria[$fields[0]];

            $this->context->buildViolation($constraint->message)
                ->atPath($errorPath)
                ->setInvalidValue($invalidValue)
                ->addViolation()
            ;
        }
    }
509628
protected function validateMaxRows($rows, Constraint $constraint)
    {
        if (null !== $constraint->maxRows) {
            if (!ctype_digit((string) $constraint->maxRows)) {
                throw new ConstraintDefinitionException(
                    sprintf(
                        '"%s" is not a valid row size',
                        $constraint->maxRows
                    )
                );
            }

            if ($rows > $constraint->maxRows) {
                $this->context->addViolation(
                    $constraint->maxRowsMessage,
                    array(
                        '{{ max_rows }}' => $constraint->maxRows,
                    )
                );

                return true;
            }
        }
    }
1053514
public function validate($value, Constraint $constraint)
    {
        $parameters = array(
            'initial_state' => 0,
            'translation_state' => 0,
            'published_state' => 0,
            'auto_publish_from_state' => 0,
            'auto_unpublish_to_state' => 0,
        );

        foreach ($value as $status) {
            if ($status->isInitialState()) {
                $parameters['initial_state']++;
            }
            if ($status->isTranslationState()) {
                $parameters['translation_state']++;
            }
            if ($status->isPublishedState()) {
                $parameters['published_state']++;
            }
            if ($status->isAutoPublishFromState()) {
                $parameters['auto_publish_from_state']++;
            }
            if ($status->isAutoUnpublishToState()) {
                $parameters['auto_unpublish_to_state']++;
            }
        }

        foreach ($parameters as $parameter => $setCount) {
            if (0 === $setCount) {
                $label = $this->translator
                    ->trans('open_orchestra_workflow_admin.status.' . $parameter, array(), 'interface');
                $this->context
                    ->buildViolation($constraint->requiredParameterMessage)
                    ->setParameter('%parameter%', $label)
                    ->addViolation();
            } elseif ($setCount > 1) {
                $label = $this->translator
                    ->trans('open_orchestra_workflow_admin.status.' . $parameter, array(), 'interface');
                $this->context
                    ->buildViolation($constraint->uniqueParameterMessage)
                    ->setParameter('%parameter%', $label)
                    ->addViolation();
            }
        }
    }
924979
public function validate($value, Constraint $constraint)
    {
        $value = trim($value);

        parent::validate($value, $constraint);
    }
1109121
public function validate($order, Constraint $constraint): void
    {
        /** @var OrderInterface $order */
        Assert::isInstanceOf($order, OrderInterface::class);

        /** @var OrderProductEligibility $constraint */
        Assert::isInstanceOf($constraint, OrderProductEligibility::class);

        /** @var OrderItemInterface[] $orderItems */
        $orderItems = $order->getItems();

        foreach ($orderItems as $orderItem) {
            if (!$orderItem->getProduct()->isEnabled()) {
                $this->context->addViolation(
                    $constraint->message,
                    ['%productName%' => $orderItem->getProduct()->getName()]
                );
            }
        }
    }
1171271
protected function validateEscape(Constraint $constraint)
    {
        if (strlen($constraint->escape) > 2) {
            throw new ConstraintDefinitionException(sprintf('"%s" is not a valid escape', $constraint->escape));
        }
    }
1253651
public function validate(array $constraints, $data, $prettyName = 'This value')
    {
        if (isset($constraints['callback'])) {     
            call_user_func_array($constraints['callback'], array($data, $prettyName));
        }
    }
1347187
public function validate($value, Constraint $constraint)
    {
        if (!$value instanceof LanguageCreateStruct) {
            return;
        }

        try {
            $language = $this->languageService->loadLanguage($value->languageCode);
            if ($language->id == $value->getId()) {
                return;
            }

            $this->context->buildViolation($constraint->message)
                ->atPath('language_code')
                ->setParameter('%language_code%', $value->languageCode)
                ->addViolation();
        } catch (NotFoundException $e) {
            // Do nothing
        } catch (InvalidArgumentException $e) {
            // Do nothing
        }
    }