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.

Null model handling in Web Api Validation

See original GitHub issue

How 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 Validatorwith above class. But It seems Validatemethod is not getting called in case of null model.

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
Amit1150commented, May 21, 2016

@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 .

1reaction
JeremySkinnercommented, May 17, 2016

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:

How 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 Validatorwith above class. But It seems Validatemethod is not getting called in case of null model.

— You are receiving this because you are subscribed to this thread. Reply to this email directly or view it on GitHub https://github.com/JeremySkinner/FluentValidation/issues/255

Read more comments on GitHub >

github_iconTop 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 >

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