Null model handling in Web Api Validation
See original GitHub issueHow fire validate method when my model is null.
I have tried to override validate method but it doesn’t work.
public class NullReferenceValidator<T> : AbstractValidator<T>
{
public override ValidationResult Validate(T instance)
{
return instance == null
? new ValidationResult(new[] { new ValidationFailure(instance.ToString(), "Entity can not be null.", "Error") })
: base.Validate(instance);
}
}
Inherit me my Validator
with above class. But It seems Validate
method is not getting called in case of null model.
Issue Analytics
- State:
- Created 7 years ago
- Comments:7 (2 by maintainers)
Top Results From Across the Web
Model Validation in ASP.NET Web API - ASP.NET 4.x
This article shows how to annotate your models, use the annotations for data validation, and handle validation errors in your web API.
Read more >Clean up your Web API controllers with model validation ...
Clean up your Web API controllers with model validation and null check filters. Regardless of what kind of applications you build with Web...
Read more >Validating Form Data For null values in web api
Validating Form Data For null values in web api ... It looks like model binding error. Your action method must take MyClass like...
Read more >Web API Validation
This article acquaints you with Web API Validation, Web API Model validation, ... The common thing I will do in validation is null...
Read more >ASP.NET Web API 2 - ModelState.IsValid true with Null Model
Which basically means that a null model will always be valid regardless of its data annotations, which seems a bit ill-defined don't you...
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
@nicklbailey : I did the same. But I want is there any I construct an object, if it is null then again call the validate method or something like this .
Hi,
That’s actually the correct behaviour. FluentValidation is designed to validate the properties of an object (eg a viewmodel/input model scenario where the object represents the state of a screen), so it must be non-null in order to run. You need to ensure the object is not null before passing it to FluentValidation.
On Tuesday, 17 May 2016, Amit1150 notifications@github.com wrote: