Executing a Validator

With your validator built via ValidatorBuilder<T> you can now start validating values.

A Validator<T> is returned from the Build() method of ValidatorBuilder<T>


Method
Validate(T value, ValidationOptions options = null)
Returns
ValidationResult

Example Usage

Building and executing a Validator

// build a validator
var validator = new ValidatorBuilder<Employee>()
    .Required(e => e.FirstName)
    .For(e => e.LastName, name => 
    {
        name.Required()
            .MaxLength(30);
    })
    .Email(e => e.Email)
    .Range(e => e.Salary, 50000, 100000)
    .LessThanOrEqual(e => e.Commenced, DateTime.Today)
    .Continue(v =>
    {
        v.True(e => e.Active);
    })
    .Build();

// object to validate
var employee = new Employee
{
    FirstName = "Homer",
    Email = "homer@springfieldnuclear.com",
    Salary = 45000
};

// execute the validator
var result = validator.Validate(employee, new ValidationOptions { ThrowWhenInvalid = true });

See also