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.

Validators of IEnumerable not executed

See original GitHub issue

FluentValidation version

10.3.0

ASP.NET version

.Net 5

Summary

I have a basic implementation of WeatherForecast sample from a new API project that I am using as example project for various things such as FluentValidation and other standards in team projects.

I create a controller that takes an IEnumerable of WeatherForecast model as a request body.

The exact same code and validator setup works when the Validator is set as implementing AbstractValidator<List<WeatherForecast>> but fails when implementing as AbstractValidator<IEnumerable<WeatherForecast>>.

Initially before I figured out that it was the IEnumerable vs List type I thought it might have been a registration or configuration issue because it was not getting hit in the debugger.

I tried to register the validator as follows without any difference. options.RegisterValidatorsFromAssemblyContaining<WeatherForcastValidator>();

I also tried to set various configuration combinations on the validators but none seemed to change this behavior. ImplicitlyValidateChildProperties AutomaticValidationEnabled; ImplicitlyValidateRootCollectionElements

It seems unexpected that IEnumerable, List, or another collection types would break the validator like this.

Steps to Reproduce

I have a basic model of WeatherForecast defined as

public class WeatherForecast
{
    public DateTime Date { get; set; }
    public string Summary { get; set; }
    public int? TemperatureC { get; set; }
    public int? TemperatureF { get; set; }
}

And a Controller accepting this model as a POST Body like

[HttpPost]
public async Task<ActionResult<IEnumerable<WeatherForecast>>> Post([FromBody] IEnumerable<WeatherForecast> weatherForecasts)
{
    return CreatedAtAction(nameof(Get), await WeatherForecastService.PostForecast(weatherForecasts));
}

And the validator defined as

public class WeatherForcastValidator : AbstractValidator<IEnumerable<WeatherForecast>>
{
    public WeatherForcastValidator()
    {
        RuleForEach(weatherForecast => weatherForecast).ValidTemperatures();
        RuleForEach(weatherForecast => weatherForecast).ValidForecastDateRange(4);
    }
}

While registering the Validators in Startup.cs as

  services.AddControllers()
      .AddFluentValidation(options =>
      {
             options.RegisterValidatorsFromAssembly(typeof(WeatherForcastValidator).Assembly);
      });

If the AbstractValidator base class is defined with type as AbstractValidator<IEnumerable<WeatherForecast>> the WeatherForecastValidator constructor will never be hit in the debugger and no validation will occur.

If simply changing the IEnumerable to List in the defined type such as AbstractValidator<List<WeatherForecast>> the WeatherForecastValidator constructor is hit in the debugger and validation will occur as expected.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
JeremySkinnercommented, Aug 4, 2021

You’d need to fork the repository (using the GitHub fork button), and then push to your fork. You’ll then be able to open a PR from there

generally when contributing to an oss project you don’t push to the origin directly, but contributors use their own forks instead (unless part of the project’s core team. hope that makes sense!

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Custom Validation Not Executing
I have implemented the IValidatableObject interface to provide the custom validation logic. The problem is that my IEnumerable<ValidationResult> ...
Read more >
Trouble validating IEnumerable<TModel> · Issue #349
I have tried to create an AbstractValidator<MyModel> , but that did not validate anything. I put a breakpoint in the contstructor of my ......
Read more >
Ruleset validation not working when root model is a ...
Collection validators are only working when controller actions take a List, not working on IEnumerable. This is expected.
Read more >
Built-in Validators — FluentValidation documentation
When used on an IEnumerable (such as arrays, collections, lists, etc.), the validator ensures that the IEnumerable is not empty. Example: RuleFor(customer => ......
Read more >
Don't Fall into the IEnumerable&lt;T&gt; Trap - | juri.dev
When the IEnumerable was accessed 1st in line 5, the method GetAllValidations() was called, actually executing the enumeration with new ...
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