symfony-docs icon indicating copy to clipboard operation
symfony-docs copied to clipboard

[Validator] About Assert alias for constraints

Open gitomato opened this issue 11 months ago • 3 comments

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!

gitomato avatar Mar 07 '24 10:03 gitomato

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.

xabbuh avatar Mar 07 '24 11:03 xabbuh

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;

gitomato avatar Mar 07 '24 14:03 gitomato

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 😄

alexandre-daubois avatar Mar 12 '24 07:03 alexandre-daubois