EnsureValidation 1.0.0
dotnet add package EnsureValidation --version 1.0.0
NuGet\Install-Package EnsureValidation -Version 1.0.0
<PackageReference Include="EnsureValidation" Version="1.0.0" />
<PackageVersion Include="EnsureValidation" Version="1.0.0" />
<PackageReference Include="EnsureValidation" />
paket add EnsureValidation --version 1.0.0
#r "nuget: EnsureValidation, 1.0.0"
#:package EnsureValidation@1.0.0
#addin nuget:?package=EnsureValidation&version=1.0.0
#tool nuget:?package=EnsureValidation&version=1.0.0
EnsureValidation
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()andCnpj().
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
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Write tests for your changes
- Ensure all tests pass (
dotnet test) - Submit a Pull Request
License
This project is licensed under the MIT License - see the LICENSE file for details.
| Product | Versions 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. |
-
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 |