Controllers that take [FromQuery] types that are TypeConverters do not get validated when I use AbstractValidators.
See original GitHub issueI have a controller action like so:
public async Task<ActionResult<List<ContactViewModel>>> Get([FromQuery]EntityIdList Id)
=> await _genericGet.GetMultipleByIdAsync(Id);
Where EntityIdList
is this:
[TypeConverter(typeof(EntityIdListTypeConverter))]
public class EntityIdList : List<int>
{
public EntityIdList(IEnumerable<int> integers) : base(integers) { }
}
The TypeConverter is a simple EntityIdListTypeConverter : TypeConverter
which takes 1,2,3
and gives you List<int>(){1,2,3}
. If the code is needed I can include it, but I removed for brevity.
This works with MVC without any further tweaks. Now, I moved to create an AbstractValidator
like so:
public class EntityIdListValidator : AbstractValidator<EntityIdList> // I tried List<int> as well.
{
public EntityIdListValidator()
{
RuleFor(x => x)
.NotNull();
}
}
But this validator never runs. My controller passes a null EntityIdList Id
off to my service. I have other validators working so I know that my registrations are not the issue.
What am I missing?
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Controllers that take [FromQuery] types that are ...
Controllers that take [FromQuery] types that are TypeConverters do not get validated when I use AbstractValidators. #977.
Read more >Why isn't FromQuery working in my ASP.Net Core 1.1 ...
in PostMan with the GET verb, I get the list of service type codes, as expected. However, If I try to add anything...
Read more >TypeConverter Class (System.ComponentModel)
Provides a unified way of converting types of values to other types, as well as for accessing standard values and subproperties.
Read more >Getting a complex type as a simple type from the query ...
As we can see above, created a class inheriting from TypeConverter , implemented the conversion from string to SomeWrapperType , and finally ......
Read more >Referencing complex data using Room
You support custom types by providing type converters, which are methods that tell Room how to convert custom types to and from known...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks for the example - this makes more sense to me now. I missed something important when I looked over your example. FluentVaidation cannot be run against a null instance.
FluentValidation is designed to validate properties of an object. The root instance itself cannot be null. In this case, FluentValidation will never be invoked as there is nothing to pass to it. If the EntityIds was a property of a different object with a NotNull rule against it, it would work fine but if you want to check that a root model is not null then you will need to do this outside FluentValidation (probably in the controller action).
Ah, yes I am using
[ApiController]
.@JeremySkinner here is a full repo https://github.com/VictorioBerra/fv-issue-977
All I did was
dotnet new webapi && dotnet add package fluentvalidation.aspnetcore
and then I added my typeconverter and wired up FV according to the docs.You can see the values controller has two actions, one shows a complex object working perfectly with its abstractvalidator. The other using my EntityIdList. Even though I specified a notnull rule, it does not kick in. The controller action starts processing the request.