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.

The 2nd fluent validator is not called when api method has 2 request models

See original GitHub issue

Which version of FluentValidation are you using? <PackageReference Include="FluentValidation.AspNetCore" Version="8.6.2" />

Which version of ASP.NET are you using? <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="3.1.3" />

Describe the issue that you’re having Do you support fluent validation for this kind of controller method signature?

[HttpPut("api/content/{providerId}/{releaseAssetId}/movies")]
public async Task<ActionResult> PutMovieAsset([FromRoute] ReleaseRequestModel routeModel, 
            [FromBody] PutMovieAssetRequestModel model, CancellationToken cancellationToken)

The problem is that fluent validator of ReleaseRequestModel is called but the one for PutMovieAssetRequestModel is not.

Validators are all defined in the same project as Startup, but in their own subdirectories:

.AddFluentValidation(options =>
                {
                    options.RunDefaultMvcValidationAfterFluentValidationExecutes = true;
                    options.ImplicitlyValidateChildProperties = true;

                    options.RegisterValidatorsFromAssemblyContaining<Startup>();
                })

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
JeremySkinnercommented, Nov 10, 2020

Hi,

I just tried this and can’t reproduce the problem - it worked correctly for me. Both validators were instantiated and executed, and both sets of validation failures were added to ModelState.

Please put together a small sample application that reproduces the issue (ideally in a github repo that I can pull down) then I’ll be able to investigate further. Thanks.

0reactions
lnaiecommented, Nov 10, 2020

Thanks for this.

The problem is your PutMovieAssetRequestModelValidator has an open generic type parameter - this won’t be registered with the container or instantiated automatically, as there’s no way to know automatically what type should be used. You need to remove the open generic - once removed it’ll work correctly.

// Change from
public class PutMovieAssetRequestModelValidator<T> : PutStreamableRequestModelValidator<PutMovieAssetRequestModel> { }
// to
public class PutMovieAssetRequestModelValidator : PutStreamableRequestModelValidator<PutMovieAssetRequestModel> { }

That makes sense indeed, top level validators don’t have to use generic types. Leftover from some previous code changes. Thanks

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fluent Validation not validating on request
I've updated the object, so this should be getting hit. I added the CreditLimit as 101. Why isn't this working in the pipeline...
Read more >
Built-In, Nested, Custom Validators with FluentValidation
In this article we are going to learn more about different Validators with FluentValidation, that we can use to protect our app from...
Read more >
Built-in Validators — FluentValidation documentation
FluentValidation ships with several built-in validators. ... Ensures that the specified property is not null, an empty string or whitespace (or the default ......
Read more >
Conditions — FluentValidation documentation
By default FluentValidation will apply the condition to all preceding validators in the same call to RuleFor . If you only want the...
Read more >
Using Fluent Validation in ASP.NET Core
The GO-TO Approach for Model validation in any .NET Application is Data Annotations, where you have to declare attributes over the property of...
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