Comparison Functions

These functions can be used on IComparable types.


GreaterThan

Ensures the value being validated is greater than a specified value.

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

Value must be greater than '{other}'.

Example Usage

Using type instance.

var validator = new ValidatorBuilder<int>()
    .GreaterThan(18)
    .Build();

var result = validator.Validate(16);

Using selector expression.

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

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

Using For function.

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

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

GreaterThanOrEqual

Ensures the value being validated is greater than or equal to a specified value.

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

Value must be greater than or equal to '{other}'.

Example Usage

Using type instance.

var validator = new ValidatorBuilder<int>()
    .GreaterThanOrEqual(18)
    .Build();
  
var result = validator.Validate(16);

Using selector expression.

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

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

Using For function.

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

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

LessThan

Ensures the value being validated is less than a specified value.

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

Value must be less than '{other}'.

Example Usage

Using type instance.

var validator = new ValidatorBuilder<int>()
    .LessThan(18)
    .Build();
  
var result = validator.Validate(16);

Using selector expression.

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

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

Using For function.

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

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

LessThanOrEqual

Ensures the value being validated is less than or equal to a specified value.

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

Value must be less than or equal to '{other}'.

Example Usage

Using type instance.

var validator = new ValidatorBuilder<int>()
    .LessThanOrEqual(18)
    .Build();
  
var result = validator.Validate(16);

Using selector expression.

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

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

Using For function.

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

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

Range

Ensures the value being validated is within (inclusive) a specified value.

Methods
Range<T>(T lower, T upper)
Range<T, P>(Expression<Func<T, P>> selector, P lower, P upper)
Broken Rule

Value must be in range '{lower}' to '{upper}'.

Example Usage

Using type instance.

var validator = new ValidatorBuilder<int>()
    .Range(18, 30)
    .Build();
  
var result = validator.Validate(24);

Using selector expression.

var validator = new ValidatorBuilder<Employee>()
    .Range(e => e.Age, 18, 30)
    .Build();

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

Using For function.

var validator = new ValidatorBuilder<Employee>()
    .For(e => e.Age, v => v.Range(18, 30))
    .Build();

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

Compare

Ensures two resolved values from the target of selector expressions are equal.

Methods
Compare<T, P>(
Expression<Func<T, P>> leftSelector,
Expression<Func<T, P>> rightSelector
)

Broken Rule

Value must be equal to value of {rightSelector}.

Example Usage

Using selector expressions.

var validator = new ValidatorBuilder<LoginModel>()
    .Compare(m => m.Password, m => m.PasswordConfirm)
    .Build();

var result = validator.Validate(new LoginModel 
{ 
    Password = "StrongP@ssw0rd",
    PasswordConfirm = "StrongPassw0rd"
});

CompareGreaterThan

Ensures the value of the target of the first selector expression is greater than the value of the target of the second selector expression.

Methods
CompareGreaterThan<T, P>(
Expression<Func<T, P>> leftSelector,
Expression<Func<T, P>> rightSelector
)

Broken Rule

Value must be greater than value of {rightSelector}.

Example Usage

Using selector expressions.

var validator = new ValidatorBuilder<TwoNumbersModel>()
    .CompareGreaterThan(m => m.Number1, m => m.Number2)
    .Build();

var result = validator.Validate(new TwoNumbersModel 
{ 
    Number1 = 8,
    Number2 = 7
});

CompareGreaterThanOrEqual

Ensures the value of the target of the first selector expression is greater than or equal to the value of the target of the second selector expression.

Methods
CompareGreaterThanOrEqual<T, P>(
Expression<Func<T, P>> leftSelector,
Expression<Func<T, P>> rightSelector
)

Broken Rule

Value must be greater than or equal to value of {rightSelector}.

Example Usage

Using selector expressions.

var validator = new ValidatorBuilder<TwoNumbersModel>()
    .CompareGreaterThanOrEqual(m => m.Number1, m => m.Number2)
    .Build();

var result = validator.Validate(new TwoNumbersModel 
{ 
    Number1 = 8,
    Number2 = 7
});

CompareLessThan

Ensures the value of the target of the first selector expression is less than the value of the target of the second selector expression.

Methods
CompareLessThan<T, P>(
Expression<Func<T, P>> leftSelector,
Expression<Func<T, P>> rightSelector
)

Broken Rule

Value must be less than value of {rightSelector}.

Example Usage

Using selector expressions.

var validator = new ValidatorBuilder<TwoNumbersModel>()
    .CompareLessThan(m => m.Number1, m => m.Number2)
    .Build();

var result = validator.Validate(new TwoNumbersModel 
{ 
    Number1 = 7,
    Number2 = 8
});

CompareLessThanOrEqual

Ensures the value of the target of the first selector expression is less than or equal to the value of the target of the second selector expression.

Methods
CompareLessThanOrEqual<T, P>(
Expression<Func<T, P>> leftSelector,
Expression<Func<T, P>> rightSelector
)

Broken Rule

Value must be less than or equal to value of {rightSelector}.

Example Usage

Using selector expressions.

var validator = new ValidatorBuilder<TwoNumbersModel>()
    .CompareLessThanOrEqual(m => m.Number1, m => m.Number2)
    .Build();

var result = validator.Validate(new TwoNumbersModel 
{ 
    Number1 = 7,
    Number2 = 8
});