Skip to content
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

[Question]: How to override assertion attributes or properties in general efficiently in child classes/models #2448

Open
crtl opened this issue Feb 26, 2025 · 0 comments
Labels

Comments

@crtl
Copy link

crtl commented Feb 26, 2025

Version

4.37.1

Question

I am using DTOs to model and validate my requests using my own bundle (crtl/request-dto-resolver-bundle.
I often have similar DTOs for example for create and update requests where some fields are optional in update but required in create. Using just symfony I can simply extend the class and override validation constriants using loadValidatorMetadata:

#[RequestDTO]
class MyUpdateDto {

  /**
   * @var mixed|string|null
   */
  #[Assert\Length(max: 50)]
  public mixed $name = null;
}

#[RequestDTO]
class MyCreateDto extends MyUpdateDto {
  public static function loadValidatorMetadata(ClassMetadata $metadata): void
    {
        $metadata->addPropertyConstraint("name", new Assert\NotBlank());
    }
}

It works fine in Symfony. name is required in MyCreateDto but optional in MyUpdateDto.
The problem is that API doc does not pick up loadValidatorMetadata (because its runtime).
So to override the validation constraints I have to override the complete property which is very cumbersome:

#[RequestDTO]
class MyCreateDto extends MyUpdateDto {
    /**
   * @var mixed|string|null
   */
  #[Assert\NotBlank, Assert\Length(max: 50)]
  public mixed $name = null;
}

Is there a way to extend properties from parent classes in child classes without redefining all the options?

Additional context

No response

@crtl crtl added the question label Feb 26, 2025
@crtl crtl changed the title [Question]: How to override assertion attributes efficiently in child classes/models [Question]: How to override assertion attributes or properties in general efficiently in child classes/models Feb 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant