Pragmatic.Validation
1.0.0-alpha.6
dotnet add package Pragmatic.Validation --version 1.0.0-alpha.6
NuGet\Install-Package Pragmatic.Validation -Version 1.0.0-alpha.6
<PackageReference Include="Pragmatic.Validation" Version="1.0.0-alpha.6" />
<PackageVersion Include="Pragmatic.Validation" Version="1.0.0-alpha.6" />
<PackageReference Include="Pragmatic.Validation" />
paket add Pragmatic.Validation --version 1.0.0-alpha.6
#r "nuget: Pragmatic.Validation, 1.0.0-alpha.6"
#:package Pragmatic.Validation@1.0.0-alpha.6
#addin nuget:?package=Pragmatic.Validation&version=1.0.0-alpha.6&prerelease
#tool nuget:?package=Pragmatic.Validation&version=1.0.0-alpha.6&prerelease
Pragmatic.Validation
Zero-allocation, source-generated validation for .NET 10.
The Problem
Validation in .NET scatters across layers and styles. Inline if checks bury rules inside business
logic and get duplicated. DataAnnotations uses reflection on every call, can't do async, and handles
cross-property rules awkwardly. FluentValidation is expressive but runtime-only, and the validator
class lives apart from the model it validates. In all three, rules are scattered, reflection-based, or
disconnected from the model, and none integrates natively with a DomainAction pipeline or entity
change tracking.
The Solution
Declare rules as attributes on the model. The generator produces the exact validation code at compile time: no reflection, no runtime expression evaluation, no separate validator class.
public partial class CreateReservationRequest
{
[Required, NotWhiteSpace]
public string GuestName { get; init; } = "";
[Required, Email]
public string Email { get; init; } = "";
[FutureDate]
public DateTimeOffset CheckIn { get; init; }
[GreaterThanProperty(nameof(CheckIn))]
public DateTimeOffset CheckOut { get; init; }
[Positive, Range(1, 20)]
public int NumberOfGuests { get; init; } = 1;
}
The generator emits ISyncValidator.Validate() with inline if statements. For database checks,
implement IAsyncValidator<T> with [Validator]: the generator wires up DI and a
CompositeValidator<T>. A DomainAction's pipeline runs validation before Execute() by default;
[Validate] only changes that default and [NoValidation] switches it off.
Two levels
- Level 1 (input): validates input DTOs before any database access: sync attributes first, then async validators (uniqueness, external checks).
- Level 2 (entity): validates entity invariants after a mutation is applied, before persistence; change-aware, so only modified properties are re-validated.
Installation
dotnet add package Pragmatic.Validation
dotnet add package Pragmatic.SourceGenerator # the analyzer that generates the validators
Choosing the right attribute
All attributes live in Pragmatic.Validation.Attributes. Full details + semantics:
Attribute Reference.
| Category | Attributes | Use when |
|---|---|---|
| Presence | [Required], [NotEmpty], [NotWhiteSpace] |
Value must exist / not be blank |
| String | [MinLength], [MaxLength], [Length], [Regex], [Email], [Url], [Phone], [CreditCard], [Guid] |
String format or length |
| Numeric | [Range], [GreaterThan(OrEqual)], [LessThan(OrEqual)], [Positive], [Negative] |
Constraining numbers |
| Money | [PositiveMoney], [NonNegativeMoney], [SupportedCurrency] |
Amount sign / accepted currency |
| Date | [FutureDate], [PastDate] |
Date must be past/future |
| Collection | [MinCount], [MaxCount], [Count], [ValidateElements] |
Collection size / per-element |
| Cross-property | [EqualTo], [NotEqualTo], [GreaterThan(OrEqual)Property], [LessThan(OrEqual)Property], [RequiredIf], [RequiredIfNot] |
Compare / conditionally require |
| Enum/Set | [ValidEnum], [OneOf] |
Must be a defined enum member / allowed value |
| Custom (sync) | extend ValidationAttribute |
Custom synchronous logic |
| Async | implement IAsyncValidator<T> + [Validator] |
Database / external service checks |
Quick Start
Decorate a partial class; the generator emits Validate():
using Pragmatic.Validation.Attributes;
public partial class RegisterGuestRequest
{
[Required, NotWhiteSpace] public string FirstName { get; init; } = "";
[Required, Email] public string Email { get; init; } = "";
[Range(18, 120)] public int Age { get; init; }
}
// Run it directly…
var result = request.Validate();
// …or let a DomainAction run L1+L2: it does by default, no attribute needed:
[DomainAction]
public partial class RegisterGuest : DomainAction<Guid> { /* ... */ }
Full walkthrough: Getting Started.
Status
Stable within 1.0.0-alpha: the attribute set, sync/async validators, and the L1/L2 pipeline are settled. See the roadmap.
Documentation
| Guide | What you'll learn |
|---|---|
| Concepts | L1/L2 model, sync vs async, change-aware validation, decision guide |
| Getting Started | Your first validated DTO and the [Validate] pipeline |
| Attribute Reference | Every built-in attribute, parameters, generated code, diagnostics |
| Custom Validators | IAsyncValidator<T>, [Validator], CompositeValidator<T>, custom ValidationAttribute |
| Common Mistakes | The most frequent validation pitfalls |
| Troubleshooting | Problem/solution guide with diagnostics |
Requirements
- .NET 10.0+
Pragmatic.SourceGeneratoranalyzer
License
Part of the Pragmatic.Design ecosystem. See Licensing. Pragmatic.Validation is MIT-licensed.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. 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. net11.0 is compatible. |
-
net10.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.10)
- Microsoft.Extensions.Options (>= 10.0.10)
- Pragmatic.Ensure (>= 1.0.0-alpha.6)
- Pragmatic.Result (>= 1.0.0-alpha.6)
-
net11.0
- Pragmatic.Ensure (>= 1.0.0-alpha.6)
- Pragmatic.Result (>= 1.0.0-alpha.6)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on Pragmatic.Validation:
| Package | Downloads |
|---|---|
|
Pragmatic.Persistence
Persistence abstractions for Pragmatic.Design - Entity definitions, Repository pattern, UnitOfWork, and DTO-driven Query/Mutation patterns. Database-agnostic, works with any ORM. |
|
|
Pragmatic.Actions
DomainAction pattern with Pipeline, Filters, and Boundary support. Zero reflection, compile-time safe, source-generated. |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.0.0-alpha.6 | 311 | 11/21/2026 |
| 1.0.0-alpha.5 | 238 | 11/21/2026 |
| 1.0.0-alpha.4 | 294 | 11/16/2026 |
| 1.0.0-alpha.3 | 638 | 10/17/2026 |
| 1.0.0-alpha.2 | 230 | 10/12/2026 |
| 1.0.0-alpha.1 | 448 | 10/3/2026 |