ZeroAlloc.Validation by Marcel Roozekrans
NuGet / site data
Details
Info
Name: ZeroAlloc.Validation
Package Description
Author: Marcel Roozekrans
NuGet: https://www.nuget.org/packages/ZeroAlloc.Validation/
You can find more details at https://github.com/ZeroAlloc-Net/ZeroAlloc.Validation
Source: https://github.com/ZeroAlloc-Net/ZeroAlloc.Validation
Author
Marcel Roozekrans

Original Readme
ZeroAlloc.Validation
Source-generated, attribute-based alternative to FluentValidation for .NET: no reflection, 0 bytes allocated when a model is valid, and NativeAOT-ready.
Why ZeroAlloc.Validation
- Nothing allocated on the valid path. A passing validation allocates 0 B, asserted by allocation regression tests for flat, nested and collection models. FluentValidation 12 allocates 696–3,528 B for the same models.
- 22.7–145.0× faster than FluentValidation 12 on the valid path, measured side by side in the same BenchmarkDotNet run.
- Works under NativeAOT and trimming. The validator is plain generated C#, with no reflection or expression trees; CI publishes and runs a NativeAOT sample on every build.
- Mistakes surface at compile time. A numeric rule on a string, an
[IsInEnum]on a non-enum or a[CustomValidation]method with the wrong signature fails the build, not a request.
Install
dotnet add package ZeroAlloc.Validation
The source generator ships inside this package; nothing else is required.
The runtime packages target .NET 10 and .NET 11.
Quick start
using System;
using ZeroAlloc.Validation;
var validator = new CreateOrderRequestValidator(); // generated at build time
var result = validator.Validate(new CreateOrderRequest \{ Reference = "", Amount = 0, Email = "nope" });
if (!result.IsValid)
foreach (ref readonly var f in result.Failures)
Console.WriteLine($"{f.PropertyName}: {f.ErrorMessage}");
[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; \} = "";
}
Mark a model with [Validate] and the generator writes CreateOrderRequestValidator for you. Running it prints:
Reference: Reference must not be empty.
Amount: Amount must be greater than 0.
Email: Email must be a valid email address.
What you get
- 20 built-in rules as attributes: emptiness, equality, ranges, lengths, regex, e-mail, enums, precision and scale.
- Your own rules through reusable attributes, inline
[Must]predicates or[CustomValidation]methods. - Asynchronous rules, awaited by the generated
ValidateAsync. - Nested objects and collections, with property paths such as
Items[0].Skuin the failures. - Rules inherited from base types, conditional rules with
When,Unlessand[SkipWhen], and short-circuiting with[StopOnFirstFailure]. - Per-rule severity,
Error,WarningorInfo, and custom error messages with placeholders. - Pipeline behaviors that wrap validation calls, without a per-call allocation.
Optional packages:
ZeroAlloc.Validation.AspNetCore: validates request models automatically and returns HTTP 422 on failure.ZeroAlloc.Validation.Inject: registers every validator in DI with oneAddZeroAllocValidators()call.ZeroAlloc.Validation.Options: validatesMicrosoft.Extensions.Optionsat startup withValidateWithZeroAlloc().ZeroAlloc.Validation.Testing: fluent assertions for unit-testing validators.
Performance
| Scenario | Speedup vs FluentValidation 12 | Allocated (valid) |
|---|---|---|
| Flat model | 22.7× | 0 B vs 664 B |
| Nested model | 100.3× | 0 B vs 1,488 B |
| Collection, 3 items | 145.0× | 0 B vs 3,528 B |
Valid path, FluentValidation 12.2.0, measured 2026-10-21. Allocations are exact. Speed is shown as same-run ratios; absolute timings are not published. On the invalid path ZeroAlloc is 20.9–129.0× faster and allocates 13.0–77.5× less. See Performance for the full results.
Learn more
- Documentation: validation.zeroalloc.net, including the attribute reference and the diagnostics list.
- Upgrading from v1: Migrating to v2 and the changelog.
Related ZeroAlloc packages:
- ZeroAlloc.Mediator (docs): its
ZeroAlloc.Mediator.Validationpackage runs these validators on every request before the handler. - ZeroAlloc.Results (docs): a Mediator request that returns
Result<T, ValidationError>gets a failed validation back as a failure result, not an exception. - ZeroAlloc.Pipeline (docs): the pipeline-behavior model this library's validator behaviors are built on.
- ZeroAlloc.Authorization (docs): plugs into the same Mediator pipeline and runs before validation, so a denied caller sees no validation errors.
Contributions are welcome: open an issue or a pull request. Licensed under MIT.
About
Generating attribute-based validators without reflection
How to use
Example (source csproj, source files)
- CSharp Project
- Person.cs
- Program.cs
This is the CSharp Project that references ZeroAlloc.Validation
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="ZeroAlloc.Validation" Version="3.0.1" />
</ItemGroup>
</Project>
This is the use of ZeroAlloc.Validation in Person.cs
using ZeroAlloc.Validation;
[Validate]
public class Person
{
[NotEmpty, MaxLength(50)]
public string FirstName \{ get; set; \} = "";
[NotEmpty, MaxLength(50)]
public string LastName \{ get; set; \} = "";
}
This is the use of ZeroAlloc.Validation in Program.cs
var validator = new PersonValidator();
var person = new Person \{ FirstName = "Ignat", LastName = "Andrei" };
var result = validator.Validate(person);
Console.WriteLine($"{person.FirstName} {person.LastName} is valid: {result.IsValid}");
var invalidPerson = new Person \{ FirstName = "", LastName = "" };
var invalidResult = validator.Validate(invalidPerson);
Console.WriteLine($"Empty names are valid: {invalidResult.IsValid}");
foreach (ref readonly var failure in invalidResult.Failures)
{
Console.WriteLine($"{failure.PropertyName}: {failure.ErrorMessage}");
}
Generated Files
Those are taken from $(BaseIntermediateOutputPath)\GX
- PersonValidator.g.cs
// <auto-generated />
#nullable enable
using ZeroAlloc.Validation;
/// <summary>Validates <c>Person</c> instances against the rules declared on the type.</summary>
public sealed partial class PersonValidator : ValidatorFor<global::Person>
{
/// <inheritdoc/>
public override global::ZeroAlloc.Validation.ValidationResult Validate(global::Person instance)
{
var _buf = new global::ZeroAlloc.Validation.Internal.FailureBuffer(4);
if (string.IsNullOrEmpty(instance.FirstName))
{
_buf.Add(new global::ZeroAlloc.Validation.ValidationFailure \{ PropertyName = "FirstName", ErrorMessage = "FirstName must not be empty." });
}
if (instance.FirstName is not null && (instance.FirstName.Length > 50))
{
_buf.Add(new global::ZeroAlloc.Validation.ValidationFailure \{ PropertyName = "FirstName", ErrorMessage = "FirstName must not exceed 50 characters." });
}
if (string.IsNullOrEmpty(instance.LastName))
{
_buf.Add(new global::ZeroAlloc.Validation.ValidationFailure \{ PropertyName = "LastName", ErrorMessage = "LastName must not be empty." });
}
if (instance.LastName is not null && (instance.LastName.Length > 50))
{
_buf.Add(new global::ZeroAlloc.Validation.ValidationFailure \{ PropertyName = "LastName", ErrorMessage = "LastName must not exceed 50 characters." });
}
return _buf.ToResult();
}
}
Useful
Download Example (.NET C#)
Share ZeroAlloc.Validation
https://ignatandrei.github.io/RSCG_Examples/v2/docs/ZeroAlloc.Validation
Category "Validator" has the following generators:
1 validly
2025-10-06
2 ZeroAlloc.Validation
2026-11-21