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

FluentValidation not triggered #1153

Open
zinov opened this issue Mar 26, 2024 · 2 comments
Open

FluentValidation not triggered #1153

zinov opened this issue Mar 26, 2024 · 2 comments

Comments

@zinov
Copy link

zinov commented Mar 26, 2024

Describe the bug
I am trying to set up a Validator associated with my query parameters, which are wrapped inside a class and marked as [FromQuery] inside of the action method of the controller.

My Controller looks like this one:

public async Task<ActionResult> GetVehicles([FromQuery] VehicleRequestDto requestDto)

My Dto:

public class VehicleRequestDto
{
    public int? PageNumber { get; set; }
}

my validator validator

public class VehicleRequestDtoValidator : AbstractValidator<VehicleRequestDto>
  {
      public VehicleRequestDtoValidator()
      {
          RuleFor(x => x.PageNumber)
              .GreaterThan(0)
              .WithMessage("This is my super validation message");
      }
  }

Keeping the AddFluentValidationFromAssembly(...)

but it never hits the validator before hitting the controller, also in the ValidationBehaviour I am not getting any validators

To Reproduce
Steps to reproduce the behavior:

  1. Create a Dto
    public class VehicleRequestDto
    {
    public int PageNumber { get; set; }
    }

  2. Create a Validator for the Dto instead of the query handler and try to call the endpoint

  3. It hits the controller endpoint but never triggers the validator or passes any validators to the ValidationBehaviour to get executed. Also, check that ValidationBehaviour is executing an async call to ValidateAsync when the .net core pipeline is sync.

Please also check and reconsider removing the library from the solution and use manual validation instead, eventually if this is the case, I think the ValidationBehaviour is going to go away.

FluentValidation/FluentValidation#1959
dotnet/aspnetcore#31905

@zinov zinov changed the title Is FluentValidation Working for the endpoints you create? FluentValidation not working for the endpoints you create with plain dtos intestead of queryhandlers? Mar 26, 2024
@zinov zinov changed the title FluentValidation not working for the endpoints you create with plain dtos intestead of queryhandlers? FluentValidation not working for the endpoints you create with plain dtos? Mar 26, 2024
@zinov zinov changed the title FluentValidation not working for the endpoints you create with plain dtos? FluentValidation not triggered after Model Binding Mar 27, 2024
@zinov zinov changed the title FluentValidation not triggered after Model Binding FluentValidation not triggered Mar 27, 2024
@andissanayake
Copy link

First I am not from clean architecture team, But is this issue related to this clean architecture project template. My recommendation is try set up FluentValidation in separate project. Feel like your issue is related to AddFluentValidationFromAssembly.

@mdawood1991
Copy link

I assume the validation in your case is not being hit because the ValidationBehaviour specifically only validates the classes implementing the interface: where TRequest : IRequest<TResponse>

image

Why it's not being validated at controller level - that is a separate issue.

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

No branches or pull requests

3 participants