Validators of IEnumerable not executed
See original GitHub issueFluentValidation 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:
- Created 2 years ago
- Comments:8 (4 by maintainers)
Top GitHub Comments
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!
Created a PR https://github.com/FluentValidation/FluentValidation/pull/1796