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 with Conventional Controllers? #21971

Open
lommez opened this issue Jan 22, 2025 · 2 comments
Open

FluentValidation with Conventional Controllers? #21971

lommez opened this issue Jan 22, 2025 · 2 comments

Comments

@lommez
Copy link

lommez commented Jan 22, 2025

Looks like FluentValidation doesnt work with Asp. Net conventional controllers.
There is something else that i must do to make it work?

Or should i and use the official approach of FluentValidation to setup correctly?

Thanks

@maliming
Copy link
Member

  • Your ABP Framework version.
  • Please share the steps and code to reproduce the problem.

@lommez
Copy link
Author

lommez commented Jan 23, 2025

I'm using version 9.0.3

In the solution, project HttpApi, i´ve created a controller, and endpoint input class and the the validator.
The behavior expect was to the validator be executed for that endpoint.
Here are the steps

  1. Create the input for the endpointt
public class CreateUserDto
{
  public string Name {get;set;}
}
  1. Create the validator
public class CreateUserValidator : AbstractValidator<CreateUserDto>
{
  public CreateUserValidator()
  {
      RuleFor(x => x.Name )
        .NotEmpty()
        .WithMessage("The property {PropertyName} is required");
  }
}
  1. Create the controller
[ApiVersion("1.0", Deprecated = false)]
[ApiController]
[ControllerName("user")]
[Route("api/user")]
public class UserController : MyDemoController
{
        [HttpPost()]
        public virtual async Task<IActionResult> CreateUser(FromBody] CreateUserDto input)
        {
            // do something to create the user
            return Ok();
        }
}
  1. In the HttpApi module classe use the AbpFluentValidationModule to be depends on it.

Those are the steps.

But only worked for me if i follow the official documentation of FluenValidation.
By instaling FluentValidation.AspNetCore

And in the module ConfigureServices method, execute that:

context.Services.AddValidatorsFromAssemblyContaining<CreateUserValidator >();

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

2 participants