-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
feat: Import symfony/validator #63556
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OC\Validator; | ||
|
|
||
| use OCP\Validator\Constraints\Choice; | ||
| use OCP\Validator\Constraints\Count; | ||
| use OCP\Validator\Constraints\Email; | ||
| use OCP\Validator\Constraints\Length; | ||
| use OCP\Validator\Constraints\NotBlank; | ||
| use OCP\Validator\Constraints\NotNull; | ||
| use OCP\Validator\Constraints\Range; | ||
| use OCP\Validator\Constraints\Regex; | ||
| use Symfony\Component\Validator\Constraint as SymfonyConstraint; | ||
| use Symfony\Component\Validator\Constraints as Assert; | ||
| use Symfony\Component\Validator\Mapping\ClassMetadata; | ||
| use Symfony\Component\Validator\Mapping\Loader\LoaderInterface; | ||
|
|
||
| /** | ||
| * Feeds a Symfony ClassMetadata from the OCP\Validator\Constraints attributes | ||
| * | ||
| * This mirrors what Symfony's own Symfony\Component\Validator\Mapping\Loader\AttributeLoader | ||
| * does for its own attributes, but reads OCP\Validator\Constraints\* instead, translating each | ||
| * one into the equivalent Symfony constraint, so OCP does not have to expose Symfony's | ||
| * constraint classes. | ||
| * | ||
| * Only properties are supported, accessor methods are not. | ||
| */ | ||
| class AttributeLoader implements LoaderInterface { | ||
| #[\Override] | ||
| public function loadClassMetadata(ClassMetadata $metadata): bool { | ||
| $reflectionClass = $metadata->getReflectionClass(); | ||
| $className = $reflectionClass->name; | ||
| $loaded = false; | ||
|
|
||
| foreach ($reflectionClass->getProperties() as $property) { | ||
| if ($property->getDeclaringClass()->name !== $className) { | ||
| continue; | ||
| } | ||
|
|
||
| foreach ($property->getAttributes() as $reflectionAttribute) { | ||
| $constraint = $this->buildConstraint($reflectionAttribute); | ||
| if ($constraint === null) { | ||
| continue; | ||
| } | ||
|
|
||
| $metadata->addPropertyConstraint($property->name, $constraint); | ||
| $loaded = true; | ||
| } | ||
| } | ||
|
|
||
| return $loaded; | ||
| } | ||
|
|
||
| private function buildConstraint(\ReflectionAttribute $reflectionAttribute): ?SymfonyConstraint { | ||
| $attribute = match ($reflectionAttribute->getName()) { | ||
| NotBlank::class, NotNull::class, Length::class, Email::class, Range::class, Choice::class, Regex::class, Count::class => $reflectionAttribute->newInstance(), | ||
| default => null, | ||
| }; | ||
|
|
||
| return match (true) { | ||
| $attribute instanceof NotBlank => new Assert\NotBlank( | ||
| message: $attribute->message, | ||
| allowNull: $attribute->allowNull, | ||
| groups: $attribute->groups, | ||
| ), | ||
| $attribute instanceof NotNull => new Assert\NotNull( | ||
| message: $attribute->message, | ||
| groups: $attribute->groups, | ||
| ), | ||
| $attribute instanceof Length => new Assert\Length( | ||
| exactly: $attribute->exactly, | ||
| min: $attribute->min, | ||
| max: $attribute->max, | ||
| exactMessage: $attribute->exactMessage, | ||
| minMessage: $attribute->minMessage, | ||
| maxMessage: $attribute->maxMessage, | ||
| groups: $attribute->groups, | ||
| ), | ||
| $attribute instanceof Email => new Assert\Email( | ||
| message: $attribute->message, | ||
| groups: $attribute->groups, | ||
| ), | ||
| $attribute instanceof Range => new Assert\Range( | ||
| min: $attribute->min, | ||
| max: $attribute->max, | ||
| notInRangeMessage: $attribute->notInRangeMessage, | ||
| minMessage: $attribute->minMessage, | ||
| maxMessage: $attribute->maxMessage, | ||
| groups: $attribute->groups, | ||
| ), | ||
| $attribute instanceof Choice => new Assert\Choice( | ||
| choices: $attribute->choices, | ||
| multiple: $attribute->multiple, | ||
| min: $attribute->min, | ||
| max: $attribute->max, | ||
| message: $attribute->message, | ||
| multipleMessage: $attribute->multipleMessage, | ||
| minMessage: $attribute->minMessage, | ||
| maxMessage: $attribute->maxMessage, | ||
| groups: $attribute->groups, | ||
| ), | ||
| $attribute instanceof Regex => new Assert\Regex( | ||
| pattern: $attribute->pattern, | ||
| match: $attribute->match, | ||
| message: $attribute->message, | ||
| groups: $attribute->groups, | ||
| ), | ||
| $attribute instanceof Count => new Assert\Count( | ||
| exactly: $attribute->exactly, | ||
| min: $attribute->min, | ||
| max: $attribute->max, | ||
| exactMessage: $attribute->exactMessage, | ||
| minMessage: $attribute->minMessage, | ||
| maxMessage: $attribute->maxMessage, | ||
| groups: $attribute->groups, | ||
| ), | ||
| default => null, | ||
| }; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OC\Validator; | ||
|
|
||
| use OCP\Validator\IConstraintValidator; | ||
| use OCP\Validator\Violation; | ||
| use Symfony\Component\Validator\ConstraintViolationInterface; | ||
| use Symfony\Component\Validator\Validator\ValidatorInterface; | ||
| use Symfony\Component\Validator\ValidatorBuilder; | ||
|
|
||
| class ConstraintValidator implements IConstraintValidator { | ||
| private ValidatorInterface $validator; | ||
|
|
||
| public function __construct() { | ||
| $this->validator = (new ValidatorBuilder()) | ||
| ->addLoader(new AttributeLoader()) | ||
| ->getValidator(); | ||
| } | ||
|
|
||
| #[\Override] | ||
| public function validate(mixed $data, string|array|null $groups = null): array { | ||
| $violations = $this->validator->validate($data, null, $groups); | ||
|
|
||
| return array_map( | ||
| static fn (ConstraintViolationInterface $violation): Violation => new Violation( | ||
| propertyPath: $violation->getPropertyPath(), | ||
| message: (string)$violation->getMessage(), | ||
| invalidValue: $violation->getInvalidValue(), | ||
| ), | ||
| iterator_to_array($violations), | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCP\Validator\Constraints; | ||
|
|
||
| /** | ||
| * Validates that a value is one of a given set of choices | ||
| * | ||
| * ``` | ||
| * class Person { | ||
| * #[Choice(choices: ['admin', 'member', 'guest'])] | ||
| * public string $role; | ||
| * } | ||
| * ``` | ||
| * | ||
| * @since 36.0.0 | ||
| */ | ||
| #[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD)] | ||
| final class Choice { | ||
| /** | ||
| * @param list<mixed> $choices the valid choices, must not be empty | ||
| * @param bool $multiple whether the value is an array of valid choices instead of a single one (defaults to false) | ||
| * @param int<0, max>|null $min the minimum number of valid choices, only used when `$multiple` is true | ||
| * @param positive-int|null $max the maximum number of valid choices, only used when `$multiple` is true | ||
| * @param string|null $message the error message for an invalid single choice, or null to use the built-in default | ||
| * @param string|null $multipleMessage the error message for an invalid choice in a multiple selection, or null to use the built-in default | ||
| * @param string|null $minMessage the error message when fewer than `$min` choices are given, or null to use the built-in default | ||
| * @param string|null $maxMessage the error message when more than `$max` choices are given, or null to use the built-in default | ||
| * @param string[]|null $groups the validation groups this constraint belongs to | ||
| * @since 36.0.0 | ||
| */ | ||
| public function __construct( | ||
| public readonly array $choices, | ||
| public readonly bool $multiple = false, | ||
| public readonly ?int $min = null, | ||
| public readonly ?int $max = null, | ||
| public readonly ?string $message = null, | ||
| public readonly ?string $multipleMessage = null, | ||
| public readonly ?string $minMessage = null, | ||
| public readonly ?string $maxMessage = null, | ||
| public readonly ?array $groups = null, | ||
| ) { | ||
| if (!$choices) { | ||
| throw new \InvalidArgumentException('The choices given to ' . self::class . ' cannot be empty.'); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCP\Validator\Constraints; | ||
|
|
||
| /** | ||
| * Validates a collection's element count | ||
| * | ||
| * ``` | ||
| * class Person { | ||
| * #[Count(min: 1, max: 5)] | ||
| * public array $nicknames; // an array of strings | ||
| * } | ||
| * ``` | ||
| * | ||
| * @since 36.0.0 | ||
| */ | ||
| #[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD)] | ||
| final class Count { | ||
| /** | ||
| * @param int<0, max>|null $min the minimum expected number of elements | ||
| * @param int<0, max>|null $max the maximum expected number of elements | ||
| * @param int<0, max>|null $exactly the exact expected number of elements, equivalent to setting `$min` and `$max` to the same value | ||
| * @param string|null $minMessage the error message when there are too few elements, or null to use the built-in default | ||
| * @param string|null $maxMessage the error message when there are too many elements, or null to use the built-in default | ||
| * @param string|null $exactMessage the error message when `$exactly` is set and the count differs, or null to use the built-in default | ||
| * @param string[]|null $groups the validation groups this constraint belongs to | ||
| * @since 36.0.0 | ||
| */ | ||
| public function __construct( | ||
| public readonly ?int $min = null, | ||
| public readonly ?int $max = null, | ||
| public readonly ?int $exactly = null, | ||
| public readonly ?string $minMessage = null, | ||
| public readonly ?string $maxMessage = null, | ||
| public readonly ?string $exactMessage = null, | ||
| public readonly ?array $groups = null, | ||
| ) { | ||
| if ($min === null && $max === null && $exactly === null) { | ||
| throw new \InvalidArgumentException('At least one of "min", "max" or "exactly" must be set on ' . self::class . '.'); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCP\Validator\Constraints; | ||
|
|
||
| /** | ||
| * Validates that a value is a valid email address | ||
| * | ||
| * ``` | ||
| * class Person { | ||
| * #[Email] | ||
| * public string $email; | ||
| * } | ||
| * ``` | ||
| * | ||
| * @since 36.0.0 | ||
| */ | ||
| #[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD)] | ||
| final class Email { | ||
| /** | ||
| * @param string|null $message the error message, or null to use the built-in default | ||
| * @param string[]|null $groups the validation groups this constraint belongs to | ||
| * @since 36.0.0 | ||
| */ | ||
| public function __construct( | ||
| public readonly ?string $message = null, | ||
| public readonly ?array $groups = null, | ||
| ) { | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It’s sad to flatten the iterator. Maybe foreach+yield instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
at the same time, returning a generator in a
validatemethod is not great as in most case, we want to know if the returned list is empty or not