Quantcast
Channel: Fluent Validation for .NET
Viewing all articles
Browse latest Browse all 1917

New Post: IValidator.Validate(null)

$
0
0

Hi

This isn't supported out of the box as it isn't the intended use for FluentValidation. FV works by validating the properties of an object, so assumes that you're passing in an object which is ready to be validated (ie a non-null object whose properties have the values ready to validate), so your existing approach of checking for nulls before calling validate is the correct approach. However, you could override the default behaviour to perform a null check. Example:

public class PersonValidator : AbstractValidator<Person> {
	public override FluentValidation.Results.ValidationResult Validate(ValidationContext<Person> context) {
		if(context.InstanceToValidate == null) {
			return new ValidationResult(new[] { new ValidationFailure("", "Instance cannot be null."),  });
		}
		return base.Validate(context);
	}
}

Jeremy


Viewing all articles
Browse latest Browse all 1917

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>