ZeroAlloc.Validation.Generator
1.2.0
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package ZeroAlloc.Validation.Generator --version 1.2.0
NuGet\Install-Package ZeroAlloc.Validation.Generator -Version 1.2.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="ZeroAlloc.Validation.Generator" Version="1.2.0"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ZeroAlloc.Validation.Generator" Version="1.2.0" />
<PackageReference Include="ZeroAlloc.Validation.Generator"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
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 ZeroAlloc.Validation.Generator --version 1.2.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: ZeroAlloc.Validation.Generator, 1.2.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 ZeroAlloc.Validation.Generator@1.2.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=ZeroAlloc.Validation.Generator&version=1.2.0
#tool nuget:?package=ZeroAlloc.Validation.Generator&version=1.2.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
ZeroAlloc.Validation
Source-generated, attribute-based validation for .NET that allocates nothing on the valid path. The source generator emits a strongly-typed validator class at build time — no reflection at runtime. When all rules pass, the entire validation cycle produces zero heap allocations.
Install
dotnet add package ZeroAlloc.Validation
30-Second Example
using ZeroAlloc.Validation;
[Validate]
public class CreateOrderRequest
{
[NotEmpty][MaxLength(50)] public string Reference { get; set; } = "";
[GreaterThan(0)] public decimal Amount { get; set; }
[NotEmpty][EmailAddress] public string Email { get; set; } = "";
}
// The source generator emits CreateOrderRequestValidator at build time
var request = new CreateOrderRequest
{
Reference = "ORD-2026-001",
Amount = 99.99m,
Email = "customer@example.com"
};
var validator = new CreateOrderRequestValidator();
var result = validator.Validate(request);
if (!result.IsValid)
foreach (ref readonly var f in result.Failures)
Console.WriteLine($"{f.PropertyName}: {f.ErrorMessage}");
Performance
| Scenario | ZeroAlloc.Validation | FluentValidation | Speedup | Allocation (valid) |
|---|---|---|---|---|
| Flat model | 6.7 ns | 327 ns | ~49× | 0 B |
| Nested model | 10.1 ns | 619 ns | ~61× | 0 B |
| Collection (3×) | 14.3 ns | 2,043 ns | ~143× | 0 B |
See Performance for full benchmark results.
Packages
| Package | Purpose |
|---|---|
ZeroAlloc.Validation |
Core library — attributes, source generator, ValidationResult |
ZeroAlloc.Validation.AspNetCore |
Auto-validates request models; returns HTTP 422 on failure |
ZeroAlloc.Validation.Inject |
Emits AddZeroAllocValidators() — bulk DI registration in one call |
ZeroAlloc.Validation.Options |
Emits ValidateWithZeroAlloc() — plugs validators into Microsoft.Extensions.Options |
ZeroAlloc.Validation.Testing |
Fluent assertions for unit-testing validators |
Features
- Zero heap allocation on the valid path
- 25+ built-in validation attributes
- Nested object and collection validation
- ASP.NET Core auto-validation (HTTP 422 on failure)
- Zero-friction DI registration (
AddZeroAllocValidators()) - Source-generated
Microsoft.Extensions.Optionsintegration (ValidateWithZeroAlloc()) - Per-rule severity (
Error,Warning,Info) - Conditional rules (
When/Unless/[SkipWhen]) - Short-circuit with
[StopOnFirstFailure] - Custom rules via
[Must]predicates or[CustomValidation]methods - Testing helpers via
ZeroAlloc.Validation.Testing
Documentation
There are no supported framework assets in this package.
Learn more about Target Frameworks and .NET Standard.
This package has no dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.