Create your first Validator

Building a validation function is accomplished with the ValidatorBuilder<T> class. Where T is the type you want to validate.

See The ValidatorBuilder Class.


For your first validator we will use the following Employee model.

public class Employee
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public string Phone { get; set; }
    public string[] Skills { get; set; }
    public decimal Salary { get; set; }
    public DateTime Commenced { get; set; }
    public bool Active { get; set; }
}

Our validation function must satisfy the following rules:

  • FirstName is required.
  • LastName is required and have no more than 30 characters.
  • Email must be a valid email.
  • Skills must contain Cromulent.
  • Salary between 50,000 and 100,000.
  • Commenced cannot be in the future.
  • Active must be true only if previous rules pass.

We can build a validator that will enforce the above rules like this:

var validator = new ValidatorBuilder<Employee>()
    .Required(e => e.FirstName)
    .For(e => e.LastName, name => 
    {
        name.Required()
            .MaxLength(30);
    })
    .Email(e => e.Email)
    .Contains(e => e.Skills, "Cromulent")
    .Range(e => e.Salary, 50000, 100000)
    .LessThanOrEqual(e => e.Commenced, DateTime.Today)
    .Continue(v =>
    {
        v.True(e => e.Active);
    })
    .Build();

The Build() method will return aValidator<Employee> which can be used to validate Employee instances.

var employee = new Employee
{
    FirstName = "Homer",
    Email = "homer[at]springfieldnuclear.com",
    Salary = 45000,
    Skills = new[] { "Embiggening" }
};

var result = validator.Validate(employee);

The Validate() method returns a ValidationResult instance containing a collection of broken validation rules. See Validation Results.

if (!result.IsValid)
{
    foreach (var rule in result.BrokenRules)
    {
        Console.WriteLine($"[{rule.Rule}] {rule.Key}: {rule.Message}");
    }
};

Putting it all together into an executable console application.

using System;
using Validatum;

namespace ConsoleApp
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var validator = new ValidatorBuilder<Employee>()
                .Required(e => e.FirstName)
                .For(e => e.LastName, name => 
                {
                    name.Required()
                        .MaxLength(30);
                })
                .Email(e => e.Email)
                .Contains(e => e.Skills, "Cromulent")
                .Range(e => e.Salary, 50000, 100000)
                .LessThanOrEqual(e => e.Commenced, DateTime.Today)
                .Continue(v =>
                {
                    v.True(e => e.Active);
                })
                .Build();

            var employee = new Employee
            {
                FirstName = "Homer",
                Email = "homer[at]springfieldnuclear.com",
                Salary = 45000,
                Skills = new[] { "Embiggening" }
            };

            var result = validator.Validate(employee);

            if (!result.IsValid)
            {
                foreach (var rule in result.BrokenRules)
                {
                    Console.WriteLine($"[{rule.Rule}] {rule.Key}: {rule.Message}");
                }
            };
        }

        public class Employee
        {
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public string Email { get; set; }
            public string Phone { get; set; }
            public string[] Skills { get; set; }
            public decimal Salary { get; set; }
            public DateTime Commenced { get; set; }
            public bool Active { get; set; }
        }
    }
}

Will produce the following output:

[Required] LastName: Value is required.
[Email] Email: Value must be a valid email.
[Contains] Skills: Must contain item 'Cromulent'.
[Range] Salary: Value must be in range '50000' to '100000'.