question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

CustomAsync method does not change ModelState

See original GitHub issue

I am using Asp.Net Core V3. My code looks like below:

RuleFor(x => x.ApplicationName)
    .NotEmpty().WithMessage("Application name is required to continue")
    .CustomAsync(async (appName, context, cancellationToken) =>
    {
        var application = await applicationProvider.GetLatestApplicationWithContextValuesAsync(appName, false);
        if(application != null)
            context.AddFailure($"Application with name: {appName} already exists.");
    });

I am able to debug the app and see that context.AddFailure is executed but the ModelState does not reflect the validation error.

Is this intentional or a bug ?

When some validation fail while using RuleFor the ModelState change is reflected.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
JeremySkinnercommented, May 30, 2020

Alternatively, you can leave automatic validation enabled (if you want it in some other places) and instead use [CustomizeValidator(Skip=true)] on the parameter declaration (see https://docs.fluentvalidation.net/en/latest/aspnet.html#validator-customization) to skip auto validation for specific instances.

1reaction
JeremySkinnercommented, May 30, 2020

Did not check in details how it works but my feeling is that if you call AddFluentValidation() method there is no way how to stop automatic validation from happening

Calling AddFluentValidation is what enables automatic validation. If you don’t want automatic validation, then don’t call AddFluentValidation in your startup. Instead just register the services you need directly with the service provider:

// Don't call AddFluentValidation, as this enables automatic validation.
services.AddControllers().ConfigureApiBehaviorOptions(...);

// Register the services you need.
services.AddTransient<IValidatorFactory, ServiceProviderValidatorFactory>();
services.AddControllresFromAssemblyContaining<MyValidator>();

Note that by switching to manual validation like this you also lose clientside validation integration (but this is no big loss as it’s very limited anyway). Also be aware that ASP.NET’s model binding process will also still generate some error messages prior to FluentValidation being invoked (eg if you omit a value for a non-nullable value type).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fluent Validation changing CustomAsync to MustAsync
To get around this, you can specify a custom state to be returned when failing validation.
Read more >
Creating Complex Validation Rules Using Fluent ...
TryUpdateModelAsync will update the ModelState value of the controller with results of the validation and add any validation errors to the ...
Read more >
Fluent Validation | The Machine Spirit
Testing model validation through a UI is tedious, boring, time-consuming, and is only valid immediately after you do it. Further changes require ...
Read more >
FluentValidation & Automatic Validation | by David Lebee
Fluent Validation brings an elegant solution to address those two blockers, by using a service to validate a model you can benefit from ......
Read more >
Yieldable system updates / IEnumerator world update.
Hey guys, quick question. I'm currently thinking of extending the ECS system to include yieldable system updates, where the expected ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found