These functions can be used on IComparable types.
Ensures the value being validated is greater than a specified value.
Value must be greater than '{other}'.
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 });
Ensures the value being validated is greater than or equal to a specified value.
Value must be greater than or equal to '{other}'.
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 });
Ensures the value being validated is less than a specified value.
Value must be less than '{other}'.
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 });
Ensures the value being validated is less than or equal to a specified value.
Value must be less than or equal to '{other}'.
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 });
Ensures the value being validated is within (inclusive) a specified value.
Value must be in range '{lower}' to '{upper}'.
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 });
Ensures two resolved values from the target of selector expressions are equal.
Value must be equal to value of {rightSelector}.
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"
});
Ensures the value of the target of the first selector expression is greater than the value of the target of the second selector expression.
Value must be greater than value of {rightSelector}.
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
});
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.
Value must be greater than or equal to value of {rightSelector}.
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
});
Ensures the value of the target of the first selector expression is less than the value of the target of the second selector expression.
Value must be less than value of {rightSelector}.
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
});
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.
Value must be less than or equal to value of {rightSelector}.
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
});