Skip to content

[MapRequestPayload] Allow usage of expressions for defining validation groups #58273

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

Open
wants to merge 10 commits into
base: 7.4
Choose a base branch
from

Conversation

Brajk19
Copy link
Contributor

@Brajk19 Brajk19 commented Sep 16, 2024

Q A
Branch? 7.3
Bug fix? no
New feature? yes
Deprecations? no
License MIT

This PR adds option to use Expression for defining validation groups when using #[MapRequestPayload] and
#[MapQueryString]

Inspired by expression options when using #[IsGranted], request and args are available.

Example:
We have route which is used to update user info. Entity User has property type which can be admin or regular.
If user is admin, his password must be very strong and they must have image while regular user can have weaker password and image is not required.
Payload used in request:

final readonly class UpdateUserDTO
{
    public function __construct(
        #[Assert\NotNull(message: 'Admin user must have image', groups: ['admin'])]
        public ?string $image,
        #[Assert\PasswordStrength(minScore: Assert\PasswordStrength::STRENGTH_MEDIUM, groups: ['regular'])]
        #[Assert\PasswordStrength(minScore: Assert\PasswordStrength::STRENGTH_VERY_STRONG, groups: ['admin'])]
        public string  $password,
    ) {
    }
}

We need to dynamically determine which validation group to use based on context (User that is being updated).
Current way of doing this is to manually call validate method:

$violations = $this->validator->validate($dto, groups: [$user->getType()]);
// handle violations...

This PR allows usage of Expression with access to arguments so above code can be replaced with #[MapRequestPayload]

#[Route('/user/{id}', methods: ['PUT'])]
public function updateUserAction(
    User $user,
    #[MapRequestPayload(
        validationGroups: [new Expression('args["user"].getType()')],
    )] UpdateUserDTO $dto
)

Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

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

LGTM, here are some minor comments

->set('controller.expression_language', ExpressionLanguage::class)
->args([service('cache.request_payload_value_resolver_expression_language')->nullOnInvalid()])

->set('cache.request_payload_value_resolver_expression_language')
Copy link
Member

Choose a reason for hiding this comment

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

either the EL service is named like proposed and this needs to ba aligned, or we change the name of the EL service, but the current mix looks inconsistent to me

Suggested change
->set('cache.request_payload_value_resolver_expression_language')
->set('cache.controller_expression_language')

Copy link
Contributor Author

@Brajk19 Brajk19 Oct 9, 2024

Choose a reason for hiding this comment

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

@nicolas-grekas true. i prefer suggested change.
fixed 550c8a4

@Brajk19 Brajk19 force-pushed the request-payload-value-expression-groups branch from 34d63e6 to 4af5a37 Compare October 9, 2024 18:41
@Brajk19 Brajk19 requested a review from nicolas-grekas October 9, 2024 18:42
@fabpot fabpot modified the milestones: 7.2, 7.3 Nov 20, 2024
@Brajk19 Brajk19 force-pushed the request-payload-value-expression-groups branch from 4af5a37 to 14189d2 Compare January 3, 2025 10:24
@Brajk19 Brajk19 force-pushed the request-payload-value-expression-groups branch from 5ebb49b to 1f9de26 Compare January 10, 2025 11:17
@Brajk19 Brajk19 force-pushed the request-payload-value-expression-groups branch from 1f9de26 to 29d53f3 Compare January 23, 2025 14:25
@Brajk19 Brajk19 force-pushed the request-payload-value-expression-groups branch from 29d53f3 to 85204a5 Compare March 7, 2025 21:14
@OskarStark
Copy link
Contributor

It looks like your committer email is not associated with your GitHub account, you may want to change that for credibility 🖖

@Brajk19
Copy link
Contributor Author

Brajk19 commented Mar 21, 2025

It looks like your committer email is not associated with your GitHub account, you may want to change that for credibility 🖖

@OskarStark thanks for advice, i fixed that 😄

@Brajk19 Brajk19 force-pushed the request-payload-value-expression-groups branch from 85204a5 to 67eb8ec Compare March 23, 2025 08:35
@fabpot fabpot modified the milestones: 7.3, 7.4 May 26, 2025
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