Native.FluentValidation
1.0.1
dotnet add package Native.FluentValidation --version 1.0.1
NuGet\Install-Package Native.FluentValidation -Version 1.0.1
<PackageReference Include="Native.FluentValidation" Version="1.0.1" />
<PackageVersion Include="Native.FluentValidation" Version="1.0.1" />
<PackageReference Include="Native.FluentValidation" />
paket add Native.FluentValidation --version 1.0.1
#r "nuget: Native.FluentValidation, 1.0.1"
#:package Native.FluentValidation@1.0.1
#addin nuget:?package=Native.FluentValidation&version=1.0.1
#tool nuget:?package=Native.FluentValidation&version=1.0.1
Native.FluentValidation
High-performance fluent validation library built for .NET 10 Native AOT. No reflection, no expression trees, no runtime scanning — just predictable, compile-time-friendly rules.
Features
- 🚀 Native AOT Compatible - Designed for trimming-safe, reflection-free validation
- ✅ Fluent Rules - Build rules with a predictable, compile-time API
- 🧩 Explicit Property Names -
RuleForrequiresnameof(...)for full AOT safety - 🧱 Built-in Rules -
NotNull,NotEmpty,Length,GreaterThan,Must,Email - 🧠 Rule Configuration -
WithMessage,WithErrorCode,Cascade,When/Unless - 🔗 NativeMediator Integration - Pipeline validation with
ValidationPipelineBehavior
Installation
dotnet add package Native.FluentValidation
Quick Start
1. Define a model and validator
using Native.FluentValidation.Core;
using Native.FluentValidation.Builders;
public sealed class User
{
public string? Email { get; set; }
public int Age { get; set; }
}
public sealed class UserValidator : NativeValidator<User>
{
public UserValidator()
{
RuleFor(x => x.Email, nameof(User.Email))
.NotEmpty()
.Email();
RuleFor(x => x.Age, nameof(User.Age))
.GreaterThan(17)
.WithMessage("Age must be at least 18.");
}
}
2. Validate an instance
var validator = new UserValidator();
var result = validator.Validate(new User { Email = null, Age = 16 });
if (!result.IsValid)
{
foreach (var error in result.Errors)
{
Console.WriteLine($"{error.PropertyName}: {error.ErrorMessage} ({error.ErrorCode})");
}
}
3. Integrate with NativeMediator
services.AddNativeFluentValidation<CreateUser, CreateUserValidator>();
Register multiple validators for the same request:
services.AddNativeFluentValidation(builder =>
{
builder.AddValidator<CreateUser, CreateUserValidator>();
builder.AddValidator<CreateUser, CreateUserDomainValidator>();
});
When a validator is registered, the pipeline throws ValidationException on failure.
Async Validation (opt-in)
Implement INativeAsyncValidator<T> when you need async checks:
public sealed class CreateUserAsyncValidator : INativeAsyncValidator<CreateUser>
{
public async ValueTask<ValidationResult> ValidateAsync(
CreateUser request,
CancellationToken cancellationToken = default)
{
var failures = new List<ValidationFailure>();
if (!await _repository.ExistsAsync(request.Email, cancellationToken))
{
failures.Add(new ValidationFailure(nameof(CreateUser.Email), "NotFound", "Email not found."));
}
return failures.Count == 0 ? ValidationResult.Success : new ValidationResult(failures);
}
}
Register async validators via the builder:
services.AddNativeFluentValidation(builder =>
{
builder.AddAsyncValidator<CreateUser, CreateUserAsyncValidator>();
});
Extra Rules Package
Additional rules (regex, ranges, comparisons) live in:
Native.FluentValidation.ExtraRules
NativeLambdaMediator Integration
Use LambdaMediatorFunction<TRequest, TResponse> to run NativeMediator handlers with validation in AWS Lambda:
public sealed class Function : LambdaMediatorFunction<CreateUserRequest, CreateUserResponse>
{
public Function() : base(BuildServiceProvider()) { }
private static IServiceProvider BuildServiceProvider()
{
var services = new ServiceCollection();
services.AddNativeFluentValidation<CreateUserRequest, CreateUserValidator>();
services.AddTransient<IRequestHandler<CreateUserRequest, CreateUserResponse>, CreateUserHandler>();
return services.BuildServiceProvider();
}
}
See the complete sample with deploy tooling in ../../samples/Native.FluentValidation.AwsLambda.Mediator.
Architecture
flowchart LR
Request["Request"] --> Mediator["NativeMediator"]
Mediator --> Pipeline["ValidationPipelineBehavior"]
Pipeline --> Validator["INativeValidator<T>"]
Validator --> Result["ValidationResult"]
Result --> |"Valid"| Handler["Request Handler"]
Result --> |"Invalid"| Exception["ValidationException"]
Rule Builder API
RuleFor(x => x.Name, nameof(User.Name))
.NotEmpty()
.Length(2, 100)
.WithErrorCode("E_NAME")
.WithMessage("Name is required.")
.Cascade(CascadeMode.Stop)
.When(x => x.IsActive);
Available Rules
| Rule | Description |
|---|---|
NotNull() |
Fails when value is null |
NotEmpty() |
Fails when string/collection is empty |
Length(min, max) |
Validates length range |
GreaterThan(value) |
Validates value is greater |
Must(predicate) |
Custom predicate validation |
Email() |
Validates email format (string only) |
Validation Results
Validate returns ValidationResult:
| Property | Type | Description |
|---|---|---|
IsValid |
bool |
true when no errors |
Errors |
IReadOnlyList<ValidationFailure> |
List of failures |
Each ValidationFailure includes PropertyName, ErrorCode, and ErrorMessage.
Conditional Validation
RuleFor(x => x.Email, nameof(User.Email))
.NotEmpty()
.Email()
.Unless(x => x.IsGuest);
Requirements
- .NET 10.0 or later
License
MIT License - see LICENSE for details.
| 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. |
-
net10.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.2)
- NativeMediator (>= 1.0.3)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on Native.FluentValidation:
| Package | Downloads |
|---|---|
|
Native.FluentValidation.NativeLambdaMediator
Native AOT-friendly validation integration for AWS Lambda using NativeMediator. |
|
|
Native.FluentValidation.ExtraRules
Extra validation rules for Native.FluentValidation (regex, ranges, and comparisons). |
GitHub repositories
This package is not used by any popular GitHub repositories.