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

Volo.Abp.FluentValidation Package seems to not be working #19909

Closed
1 task done
ahmedamer2 opened this issue May 22, 2024 · 4 comments
Closed
1 task done

Volo.Abp.FluentValidation Package seems to not be working #19909

ahmedamer2 opened this issue May 22, 2024 · 4 comments
Assignees

Comments

@ahmedamer2
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Description

I am currently trying to use the Volo.Abp.FluentValidation package following what is outlined in the documentation here.

Currently it seems that anything I define in my validator classes is not being picked up.

Reproduction Steps

I have created a sample project showcasing this not working here:

Github Repo

I used the started template, added one model and an appservice to showcase this behavior. I have also added API versioning as that is a requirement that I need to have working with this.

Expected behavior

FluentValidation package to detect any class using/inheriting from AbstractValidator<> and correctly triggering the rules defined.

Actual behavior

I have defined the following for My CreateBookDto Validation:

public class CreateBookValidator : AbstractValidator<CreateBookDto>
{
    public CreateBookValidator()
    {
        RuleFor(o => o.Name).NotNull().WithMessage("TEst");
        RuleFor(o => o.Price).NotEmpty();
        RuleFor(o => o.AuthorId).NotEmpty();
    }
}

So I expect to get validation errors for all of the properties here when I try creating a new Book. Instead I receive the following:

image

I don't receive errors about the missing properties and even the message for the name field is wrong. This indicates that FluentValidations are not triggering.

Regression?

No response

Known Workarounds

No response

Version

8.1.3

User Interface

Common (Default)

Database Provider

EF Core (Default)

Tiered or separate authentication server

Tiered

Operation System

Windows (Default)

Other information

No response

@ahmedamer2 ahmedamer2 added the bug label May 22, 2024
@realLiangshiwei realLiangshiwei self-assigned this May 23, 2024
@realLiangshiwei
Copy link
Member

This is because you are using the conventional controllers.

ABP uses interceptors for parameter validation, but does not intercept controllers for performance.

DynamicProxyIgnoreTypes.Add(preConfigureActions.Configure()
.ConventionalControllers
.ConventionalControllerSettings.SelectMany(x => x.ControllerTypes).ToArray());

return !DynamicProxyIgnoreTypes.Contains(type) && typeof(IValidationEnabled).IsAssignableFrom(type);

In order to use FluentValidation, you can't use a convention controller, you need to create one manually.

For example:

public class IdentityRoleController : AbpControllerBase, IIdentityRoleAppService

@realLiangshiwei
Copy link
Member

And please make the repo private

@JadynWong
Copy link
Contributor

You can add an ActionFilter to validate the model using FluentValidate.

JadynWong/AbpIssue@eec5290#diff-e7c9195c610944a0f739a7bca415e73d593bdd5712205d1b8322af4aedb8e0dd

Refer https://github.com/SharpGrip/FluentValidation.AutoValidation

Only basic tests were performed. It would be better if abp could provide built-in support.

@ahmedamer2
Copy link
Author

This solution seems to be working for now, thank you @JadynWong

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