Ensures the string value being validated is not empty.
Value cannot be empty.
Using type instance.
var validator = new ValidatorBuilder<string>()
.NotEmpty()
.Build();
var result = validator.Validate(null);
Using selector expression.
var validator = new ValidatorBuilder<Employee>()
.NotEmpty(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.NotEmpty())
.Build();
var result = validator.Validate(new Employee { LastName = "Simpson" });
Ensures the string value being validated is empty.
Value must be empty.
Using type instance.
var validator = new ValidatorBuilder<string>()
.Empty()
.Build();
var result = validator.Validate("");
Using selector expression.
var validator = new ValidatorBuilder<Employee>()
.Empty(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.Empty())
.Build();
var result = validator.Validate(new Employee { LastName = "Simpson" });
Ensures the string value being validated matches a regular expression pattern.
Value must match pattern.
Using type instance.
var validator = new ValidatorBuilder<string>()
.Regex("^[0-9]*$")
.Build();
var result = validator.Validate("12345");
Using selector expression.
var validator = new ValidatorBuilder<Employee>()
.Regex(e => e.FirstName, "^[0-9]*$")
.Build();
var result = validator.Validate(new Employee { FirstName = "Six" });
Using For function.
var validator = new ValidatorBuilder<Employee>()
.For(e => e.FirstName, v => v.Regex("^[0-9]*$"))
.Build();
var result = validator.Validate(new Employee { FirstName = "7 of 9" });
Using RegexOptions.
var validator = new ValidatorBuilder<string>()
.Regex("^[a-z]*$", RegexOptions.IgnoreCase)
.Build();
var result = validator.Validate("ABC");
Ensures the string value being validated starts with the specified value.
Value must start with '{value}'.
Using type instance.
var validator = new ValidatorBuilder<string>()
.StartsWith("fun")
.Build();
var result = validator.Validate("function");
Using selector expression.
var validator = new ValidatorBuilder<Employee>()
.StartsWith(e => e.FirstName, "Jo")
.Build();
var result = validator.Validate(new Employee { FirstName = "Homer" });
Using For function.
var validator = new ValidatorBuilder<Employee>()
.For(e => e.FirstName, v => v.StartsWith("Pet"))
.Build();
var result = validator.Validate(new Employee { FirstName = "Peter" });
Ensures the string value being validated ends with the specified value.
Value must end with '{value}'.
Using type instance.
var validator = new ValidatorBuilder<string>()
.EndsWith("on")
.Build();
var result = validator.Validate("function");
Using selector expression.
var validator = new ValidatorBuilder<Employee>()
.EndsWith(e => e.FirstName, "son")
.Build();
var result = validator.Validate(new Employee { FirstName = "Jason" });
Using For function.
var validator = new ValidatorBuilder<Employee>()
.For(e => e.FirstName, v => v.EndsWith("art"))
.Build();
var result = validator.Validate(new Employee { FirstName = "Lisa" });
Ensures the string value being validated contains the specified value.
Value must contain '{value}'.
Using type instance.
var validator = new ValidatorBuilder<string>()
.Contains("tio")
.Build();
var result = validator.Validate("function");
Using selector expression.
var validator = new ValidatorBuilder<Employee>()
.Contains(e => e.FirstName, "aso")
.Build();
var result = validator.Validate(new Employee { FirstName = "Jason" });
Using For function.
var validator = new ValidatorBuilder<Employee>()
.For(e => e.FirstName, v => v.Contains("holo"))
.Build();
var result = validator.Validate(new Employee { FirstName = "Bartholomew" });
Ensures the string value being validated has a length within the specified range.
Value must be {min} to {max} characters in length.
Using type instance.
var validator = new ValidatorBuilder<string>()
.Length(3, 8)
.Build();
var result = validator.Validate("function");
Using selector expression.
var validator = new ValidatorBuilder<Employee>()
.Length(e => e.FirstName, 1, 15)
.Build();
var result = validator.Validate(new Employee { FirstName = "Jason" });
Using For function.
var validator = new ValidatorBuilder<Employee>()
.For(e => e.FirstName, v => v.Length(1, 10))
.Build();
var result = validator.Validate(new Employee { FirstName = "Bartholomew" });
Ensures the string value being validated has a minimum length.
Value must have minimum length of {min}.
Using type instance.
var validator = new ValidatorBuilder<string>()
.MinLength(3)
.Build();
var result = validator.Validate("stop");
Using selector expression.
var validator = new ValidatorBuilder<Employee>()
.MinLength(e => e.Role, 5)
.Build();
var result = validator.Validate(new Employee { Role = "Admin" });
Using For function.
var validator = new ValidatorBuilder<Employee>()
.For(e => e.FirstName, v => v.MinLength(4))
.Build();
var result = validator.Validate(new Employee { FirstName = "Bart" });
Ensures the string value being validated has a maximum length.
Value must have maximum length of {max}.
Using type instance.
var validator = new ValidatorBuilder<string>()
.MaxLength(6)
.Build();
var result = validator.Validate("validate");
Using selector expression.
var validator = new ValidatorBuilder<Employee>()
.MaxLength(e => e.Lastname, 12)
.Build();
var result = validator.Validate(new Employee { LastName = "Smithers" });
Using For function.
var validator = new ValidatorBuilder<Employee>()
.For(e => e.FirstName, v => v.MaxLength(4))
.Build();
var result = validator.Validate(new Employee { FirstName = "El Barto" });
Ensures the string value being validated is not null or empty or whitespace.
Value is required.
Using type instance.
var validator = new ValidatorBuilder<string>()
.Required()
.Build();
var result = validator.Validate(null);
Using selector expression.
var validator = new ValidatorBuilder<Employee>()
.Required(e => e.FirstName)
.Build();
var result = validator.Validate(new Employee { FirstName = "" });
Using For function.
var validator = new ValidatorBuilder<Employee>()
.For(e => e.FirstName, v => v.Required())
.Build();
var result = validator.Validate(new Employee { FirstName = " " });
Ensures the string value being validated is an email address.
Value must be a valid email.
Using type instance.
var validator = new ValidatorBuilder<string>()
.Email()
.Build();
var result = validator.Validate("bart@example.com");
Using selector expression.
var validator = new ValidatorBuilder<Employee>()
.Email(e => e.Email)
.Build();
var result = validator.Validate(new Employee { Email = "invalid[at]email.com" });
Using For function.
var validator = new ValidatorBuilder<Employee>()
.For(e => e.Email, v => v.Email())
.Build();
var result = validator.Validate(new Employee { Email = "homer@gmail.com" });