EnsureValidation 1.0.0

dotnet add package EnsureValidation --version 1.0.0
                    
NuGet\Install-Package EnsureValidation -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="EnsureValidation" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="EnsureValidation" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="EnsureValidation" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add EnsureValidation --version 1.0.0
                    
#r "nuget: EnsureValidation, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package EnsureValidation@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=EnsureValidation&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=EnsureValidation&version=1.0.0
                    
Install as a Cake Tool

EnsureValidation

NuGet CI License: MIT

A lightweight, zero-reflection fluent validation library for .NET, designed for DDD domain entities and value objects.

Why EnsureValidation?

Feature EnsureValidation FluentValidation
Reflection None Heavy (expression trees)
Allocation Minimal Higher (validators, rules)
Integration Built into entities External validator classes
Learning curve Minimal Moderate
DDD focus First-class General purpose

EnsureValidation embeds validation directly into your domain entities using a fluent DSL -- no separate validator classes, no reflection, no magic.

Quick Start

dotnet add package EnsureValidation

1. Define your entity

using EnsureValidation;

public class Customer : Notifiable<Customer>
{
    public string? Name { get; set; }
    public string? Email { get; set; }
    public int? Age { get; set; }

    protected override void OnValidate()
    {
        Ensure.That(Name, nameof(Name))
            .Required()
            .MinLength(3)
            .MaxLength(100);

        Ensure.That(Email, nameof(Email))
            .Required()
            .EmailAddress();

        Ensure.That(Age, nameof(Age))
            .Required()
            .Between(18, 120);
    }
}

2. Validate

var customer = new Customer { Name = "AB", Email = "invalid", Age = 15 };
customer.Validate();

if (customer.IsInvalid)
{
    foreach (var notification in customer.Notifications)
        Console.WriteLine($"{notification.Field}: {notification.Message}");
}

Available Validators

String

Required() MinLength(n) MaxLength(n) ExactLength(n) ExactTrimmedLength(n) Matches(regex) EmailAddress() Must(predicate)

Numeric (int, long, decimal, double)

Required() EqualTo(n) NotEqualTo(n) GreaterThan(n) GreaterThanOrEqualTo(n) LessThan(n) LessThanOrEqualTo(n) Between(min, max) Must(predicate)

Bool

Required() IsTrue() IsFalse() Must(predicate)

Guid

Required() NotEmpty() Must(predicate)

DateTime

Required() After(date) Before(date) Between(from, to) InTheFuture() InThePast() Must(predicate)

Enum

Required() InEnum() GreaterThan(value)

Object (nested validation)

Required() IsValid() Must(predicate)

Collection

Required() AreValid()

Internationalization (i18n)

Default messages are in English. Override them globally:

Messages.To = (methodName, args) => methodName switch
{
    nameof(Messages.Required) => $"{args[0]} is required.",
    nameof(Messages.MinLength) => $"{args[0]} must be at least {args[1]} characters.",
    // ... other messages
    _ => $"{args[0]} is invalid."
};

For Portuguese (pt-BR) messages, install the companion package:

dotnet add package EnsureValidation.PtBR

EnsureValidation.PtBR

The EnsureValidation.PtBR companion package provides:

  • Default messages in Portuguese (pt-BR) — all built-in validation messages are automatically translated.
  • Brazilian document validators — additional string validators for Cpf() and Cnpj().
dotnet add package EnsureValidation.PtBR
using EnsureValidation.PtBR;

// Apply pt-BR messages globally (call once at startup)
PtBRMessages.Configure();

// Cpf and Cnpj validators become available on string chains
Ensure.That(cpf, nameof(cpf)).Required().Cpf();
Ensure.That(cnpj, nameof(cnpj)).Required().Cnpj();

Nested Object Validation

public class Order : Notifiable<Order>
{
    public Customer? Customer { get; set; }
    public List<OrderItem>? Items { get; set; }

    protected override void OnValidate()
    {
        Ensure.ThatObject(Customer, nameof(Customer))
            .Required()
            .IsValid();

        Ensure.ThatCollection(Items, nameof(Items))
            .Required()
            .AreValid();
    }
}

Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Write tests for your changes
  4. Ensure all tests pass (dotnet test)
  5. Submit a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.
  • net7.0

    • No dependencies.
  • net8.0

    • No dependencies.
  • net9.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on EnsureValidation:

Package Downloads
EnsureValidation.PtBR

Brazilian Portuguese (pt-BR) localization and CPF/CNPJ validation extensions for EnsureValidation.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 82 3/25/2026