Skip to content

[Validator] Added support for cascade validation on typed properties #36352

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

Merged
merged 1 commit into from
Jul 31, 2020

Conversation

HeahDude
Copy link
Contributor

@HeahDude HeahDude commented Apr 5, 2020

Q A
Branch? master
Bug fix? no
New feature? yes
Deprecations? no
Tickets ~
License MIT
Doc PR TODO

This PR leverages PHP 7.4 property types to "guess" typed object members and enable default cascade validation on nested objects.

Before:

use Symfony\Component\Validator\Constraints as Assert;

class Composite
{
    /**
     * @var self[]
     *
     * @Assert\Valid
     */
    public array $children;

    /**
     * @Assert\Valid
     */
    public ?self $parent;

    /**
     * @Assert\Valid
     */
    public static ?self $root;
}

After:

use Symfony\Component\Validator\Constraints as Assert;

/**
 * @Assert\Cascade
 */
class Composite
{
    /*
     * @var self[]
     */
    public array $children;
    public ?self $parent;
    public static ?self $root;
}

The constraint can also be used in xml, yaml, and of course raw PHP.


Question: is the naming ok, maybe we could use CascadeValid to be more explicit?

@HeahDude
Copy link
Contributor Author

Maybe we could also enforce a NotNull constraint too based on whether the type is nullable, since Valid ignores it by default.

@@ -4,6 +4,7 @@ CHANGELOG
5.1.0
-----

* added a `Cascade` constraint to ease validating typed nested objects
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"[...] validating nested typed object properties"?

@fabpot fabpot force-pushed the validator/cascade branch from e493e9d to f4679ef Compare July 31, 2020 07:44
@fabpot
Copy link
Member

fabpot commented Jul 31, 2020

Thank you @HeahDude.

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

Successfully merging this pull request may close these issues.

5 participants