symfony: [Validator] TypeValidator does not enforce desired type when value is NULL

The TypeValidator seems to be useless whenever the value being validated is NULL. It completely ignores the type I’ve configured it to enforce.

After debugging this for quite some time, it turns out the issue lies in the following line(s) of code:

https://github.com/symfony/symfony/blob/2.4/src/Symfony/Component/Validator/Constraints/TypeValidator.php#L29

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

What is the reasoning behind this short-circuit logic?

If I configure my TypeValidator to enforce a value to be of type integer (or string, or whatever), then I expect it to throw a validation error whenever the value is not of the desired type… especially when it’s NULL.

Allowing NULL to always be valid regardless of what type you’ve asked it to enforce seems to be counter-intuitive and leads to some unusual/unexpected validation behavior.

Coincidentally, I went through the history of this file and noticed that these very lines of code were once removed (2 years ago):

https://github.com/symfony/symfony/commit/474b4abd6df1b9a7ff30e84f0d83b1540c87ed19

Why have they been reintroduced? Was this a merging oversight?

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Reactions: 2
  • Comments: 27 (15 by maintainers)

Commits related to this issue

Most upvoted comments

@webmozart, Thanks so much for the explanation! At least I know the reasoning behind it now and it seems simple enough to workaround. However, I would still like to suggest that this behavior be documented (in the Validation section of the book) as I do not believe this to be ordinary behavior.

Though I completely agree with your opinion that a “value being set” vs. a “value satisfying some constraint” are generally orthogonal concepts, I do not agree that this is a reason for Validators to ignore null and empty strings.

As a user of validation frameworks, I expect the validation to do exactly what I tell it to do. If I ask it to validate that a value is an integer, than I expect it to do so – null and empty string are not integers. If I ask it to validate that a value is an Email, than I expect it do so – null and empty string are not valid emails. etc. etc. The validation framework should allow me to specify that a particular value may be null, in which case, it should not enforce any subsequent constrains… but it should not simply assume that null and empty string are okay. I’ve worked with many validation libraries (in PHP, Java, and JS) and I don’t believe I’ve seen this behavior in any of them (although, to be fair, I can’t say that I’ve looked at all of the code in those libraries).

Because a “value being set” vs. a “value satisfying some constraint” are somewhat orthogonal concepts, I think that’s a strong argument for differentiating how these are handled. Instead of all Validators always ignoring null and empty strings, perhaps a better approach would be for each validator to accept a parameters which specifies if null is okay or not. This can be added to the base Constraint class so that all Validators can benefit. That also eliminates duplicate code in the Validators.

Perhaps this is something that can be considered for Symfony 2.5.

Thanks for your consideration and allowing me to express my opinion on the matter.

Bests