Skip to content
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

[Validator] About Assert alias for constraints #19639

Open
gitomato opened this issue Mar 7, 2024 · 3 comments
Open

[Validator] About Assert alias for constraints #19639

gitomato opened this issue Mar 7, 2024 · 3 comments

Comments

@gitomato
Copy link
Contributor

gitomato commented Mar 7, 2024

In the documentation for validator constraints, for example NotBlank, constraints are aliased as Assert.

use Symfony\Component\Validator\Constraints as Assert;

#[Assert\NotBlank]
...

I don't really see benefits in this alias, and it adds an indirection (in the code but also in our brain). I find the non-alias version easier to read, understand and maintain.

use Symfony\Component\Validator\Constraints;

#[Constraints\NotBlank]
...

Is there any reason for suggesting adding this alias?

PS: Thanks for your hard work on Symfony!

@xabbuh
Copy link
Member

xabbuh commented Mar 7, 2024

The namespace is being imported to prevent potentially lots of use statements for all the constraints that you use. We do the same for Doctrine mapping attributes/annotations.

@gitomato
Copy link
Contributor Author

gitomato commented Mar 7, 2024

Thanks for your answer.
The shared namespace is really handy and we use it intensively (after teaching Rector not to replace it ;) ).
I'm challenging the alias part, aka

use Symfony\Component\Validator\Constraints as Assert;

instead of just

use Symfony\Component\Validator\Constraints;

@alexandre-daubois
Copy link
Contributor

I'd say the "assert" word really implies something that must fail if it is not true (like the assert() PHP function). I find it personally "stronger" than "constraint", but I think in any case it's some style preference.

Also, I guess the vast majority of Symfony projects use this (historic?) notation so updating it in the documentation would bring more confusion than anything 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants