Native.FluentValidation 1.0.1

dotnet add package Native.FluentValidation --version 1.0.1
                    
NuGet\Install-Package Native.FluentValidation -Version 1.0.1
                    
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="Native.FluentValidation" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Native.FluentValidation" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="Native.FluentValidation" />
                    
Project file
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 Native.FluentValidation --version 1.0.1
                    
#r "nuget: Native.FluentValidation, 1.0.1"
                    
#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 Native.FluentValidation@1.0.1
                    
#: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=Native.FluentValidation&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=Native.FluentValidation&version=1.0.1
                    
Install as a Cake Tool

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.

NuGet .NET

Features

  • 🚀 Native AOT Compatible - Designed for trimming-safe, reflection-free validation
  • Fluent Rules - Build rules with a predictable, compile-time API
  • 🧩 Explicit Property Names - RuleFor requires nameof(...) 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.

Version Downloads Last Updated
1.0.1 319 1/28/2026
1.0.0 88 1/27/2026