GitHub - bsheldrick/validatum: An open-source library for building fluent validation functions for .NET.

1 min read Original article ↗

Validatum is an open-source library for building fluent validation functions for .NET.

Install

.NET CLI

dotnet add package Validatum --version 1.1.0

Package Manager

Install-Package Validatum -Version 1.1.0

Platform Support

  • .NET Standard 2.0+
  • .NET Core 2.0+
  • .NET Framework 4.6.1+

Example

// build a validator
var validator = new ValidatorBuilder<Employee>()
    .Required(e => e.FirstName)
    .Email(e => e.Email)
    .For(e => e.LastName, name =>
    {
        name.MinLength(5)
            .Equal("Smithers");
    })
    .Build();

// validate
var result = validator.Validate(
    new Employee
    {
        LastName = "Simpson",
        Email = "homer[at]springfieldnuclear.com"
    }
);

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

Output

[Required] FirstName: Value is required.
[Email] Email: Value must be a valid email.
[Equal] LastName: Value must equal 'Smithers'.

Why use Validatum?

  • Does not require inheritance of a base type.
  • Define and build functions in the scope that validation will be executed.
  • Define very complex validation logic in a single statement.
  • Easily add custom validators as inline functions.
  • Very small package size ~33KB.
  • Simple and easily extensible API.

Documentation

Please visit https://bsheldrick.github.io/validatum