8000 [Validator] TypeValidator does not enforce desired type when value is NULL · Issue #10221 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
[Validator] TypeValidator does not enforce desired type when value is NULL #10221
Closed
@natorojr

Description

@natorojr

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):

474b4ab

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0