String Functions


NotEmpty

Ensures the string value being validated is not empty.

Methods
NotEmpty()
NotEmpty<T>(Expression<Func<T, string>> selector)
Broken Rule

Value cannot be empty.

Example Usage

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" });

Empty

Ensures the string value being validated is empty.

Methods
Empty()
Empty<T>(Expression<Func<T, string>> selector)
Broken Rule

Value must be empty.

Example Usage

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" });

Regex

Ensures the string value being validated matches a regular expression pattern.

Methods
Regex(string pattern)
Regex(string pattern, RegexOptions options)
Regex<T>(Expression<Func<T, string>> selector, string pattern)
Regex<T>(Expression<Func<T, string>> selector, string pattern, RegexOptions options)
Broken Rule

Value must match pattern.

Example Usage

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");

StartsWith

Ensures the string value being validated starts with the specified value.

Methods
StartsWith(string value)
StartsWith<T>(Expression<Func<T, string>> selector, string value)
Broken Rule

Value must start with '{value}'.

Example Usage

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" });

EndsWith

Ensures the string value being validated ends with the specified value.

Methods
EndsWith(string value)
EndsWith<T>(Expression<Func<T, string>> selector, string value)
Broken Rule

Value must end with '{value}'.

Example Usage

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" });

Contains

Ensures the string value being validated contains the specified value.

Methods
Contains(string value)
Contains<T>(Expression<Func<T, string>> selector, string value)
Broken Rule

Value must contain '{value}'.

Example Usage

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" });

Length

Ensures the string value being validated has a length within the specified range.

Methods
Length(int min, int max)
Length<T>(Expression<Func<T, string>> selector, int min, int max)
Broken Rule

Value must be {min} to {max} characters in length.

Example Usage

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" });

MinLength

Ensures the string value being validated has a minimum length.

Methods
MinLength(int min)
MinLength<T>(Expression<Func<T, string>> selector, int min)
Broken Rule

Value must have minimum length of {min}.

Example Usage

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" });

MaxLength

Ensures the string value being validated has a maximum length.

Methods
MaxLength(int max)
MaxLength<T>(Expression<Func<T, string>> selector, int max)
Broken Rule

Value must have maximum length of {max}.

Example Usage

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" });

Required

Ensures the string value being validated is not null or empty or whitespace.

Methods
Required()
Required<T>(Expression<Func<T, string>> selector)
Broken Rule

Value is required.

Example Usage

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 = "   " });

Email

Ensures the string value being validated is an email address.

Methods
Email()
Email<T>(Expression<Func<T, string>> selector)
Broken Rule

Value must be a valid email.

Example Usage

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" });