Common Functions


NotNull

Ensures the value being validated is not null.

Methods
NotNull<T>()
NotNull<T, P>(Expression<Func<T, P>> selector)
Broken Rule

Value cannot be null.

Example Usage

Using type instance.

var validator = new ValidatorBuilder<string>()
    .NotNull()
    .Build();

var result = validator.Validate(null);

Using selector expression.

var validator = new ValidatorBuilder<Employee>()
    .NotNull(e => e.FirstName)
    .Build();

var result = validator.Validate(new Employee { FirstName = "Homer" });

Using For function.

var validator = new ValidatorBuilder<Employee>()
    .For(e => e.FirstName, v => v.NotNull())
    .Build();

var result = validator.Validate(new Employee { LastName = "Simpson" });

Null

Ensures the value being validated is null.

Methods
Null<T>()
Null<T, P>(Expression<Func<T, P>> selector)
Broken Rule

Value must be null.

Example Usage

Using type instance.

var validator = new ValidatorBuilder<string>()
    .Null()
    .Build();

var result = validator.Validate(null);

Using selector expression.

var validator = new ValidatorBuilder<Employee>()
    .Null(e => e.FirstName)
    .Build();

var result = validator.Validate(new Employee { FirstName = "Homer" });

Using For function.

var validator = new ValidatorBuilder<Employee>()
    .For(e => e.FirstName, v => v.Null())
    .Build();

var result = validator.Validate(new Employee { LastName = "Simpson" });

Equal

Ensures the value being validated is equal to a specified value.

Methods
Equal<T>(T other)
Equal<T, P>(Expression<Func<T, P>> selector, P other)
Broken Rule

Value must equal '{other}'.

Example Usage

Using type instance.

var validator = new ValidatorBuilder<string>()
    .Equal("Smithers")
    .Build();

var result = validator.Validate("Smithers");

Using selector expression.

var validator = new ValidatorBuilder<Employee>()
    .Equal(e => e.FirstName, "Homer")
    .Build();

var result = validator.Validate(new Employee { FirstName = "Homer" });

Using For function.

var validator = new ValidatorBuilder<Employee>()
    .For(e => e.LastName, v => v.Equal("Smithers"))
    .Build();
    
var result = validator.Validate(new Employee { LastName = "Simpson" });

NotEqual

Ensures the value being validated is not equal to a specified value.

Methods
NotEqual<T>(T other)
NotEqual<T, P>(Expression<Func<T, P>> selector, P other)
Broken Rule

Value must not equal '{other}'.

Example Usage

Using type instance.

var validator = new ValidatorBuilder<int>()
    .NotEqual(17)
    .Build();

var result = validator.Validate(18);

Using selector expression.

var validator = new ValidatorBuilder<Employee>()
    .NotEqual(e => e.Age, 17)
    .Build();

var result = validator.Validate(new Employee { Age = 18 });

Using For function.

var validator = new ValidatorBuilder<Employee>()
    .For(e => e.Age, v => v.NotEqual(17))
    .Build();

var result = validator.Validate(new Employee { Age = 18 });