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

New Post: How to reuse validator for a base class?

$
0
0
public class BaseValidator<T> : AbstractValidator<T> where T: Base
{
    public BaseValidator()
    {
        RuleFor(data => data.Age).GreaterThan(18).WithMessage("The age must be greater than 18 years");
        RuleFor(data => data.Name).NotEmpty().WithMessage("The name must not be empty");
    }
}

public class ChildAValidator : BaseValidator<ChildA>
{
    public ChildAValidator()
    {
        RuleFor(data => data.YeardOfExperience).GreaterThan(5).WithMessage("The years of experience must be greather than 5");
    }
}

public class ChildBValidator : BaseValidator<ChildB>
{
    public ChildBValidator()
    {
        RuleFor(data => data.Ratio).LessThanOrEqualTo(1).WithMessage("The ratio must be less or equal to 1");
    }
}

Viewing all articles
Browse latest Browse all 1917

Trending Articles



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