AutoValidate.Generator 1.0.0

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

AutoValidate.Generator

NuGet CI

Compile-time FluentValidation wiring for .NET.

AutoValidate.Generator uses Roslyn source generators to automatically discover your AbstractValidator<T> subclasses and generate AddValidators() on IServiceCollection — no assembly scanning, no reflection, no runtime overhead.


Installation

dotnet add package AutoValidate.Generator
dotnet add package FluentValidation

Quick Start

Define your validators as normal:

public class Order
{
    public string CustomerName { get; set; } = "";
    public decimal Total { get; set; }
}

public class OrderValidator : AbstractValidator<Order>
{
    public OrderValidator()
    {
        RuleFor(x => x.CustomerName).NotEmpty();
        RuleFor(x => x.Total).GreaterThan(0);
    }
}

Register everything in one line — no manual wiring:

builder.Services.AddValidators();

AutoValidate discovers every non-abstract AbstractValidator<T> in your assembly at compile time and generates the registration code for you.


Attributes

[SkipValidator]

Exclude a validator from auto-registration (e.g. test validators, base classes you register manually):

[SkipValidator]
public class TestOrderValidator : AbstractValidator<Order> { }

[ValidatorLifetime]

Override the DI lifetime. Default is Scoped.

using AutoValidate;

// Singleton — validator has no mutable state
[ValidatorLifetime(ValidatorLifetime.Singleton)]
public class ConfigValidator : AbstractValidator<AppConfig> { }

// Transient — validator has per-request dependencies
[ValidatorLifetime(ValidatorLifetime.Transient)]
public class RequestValidator : AbstractValidator<CreateOrderRequest> { }

Available lifetimes: ValidatorLifetime.Scoped (default), ValidatorLifetime.Singleton, ValidatorLifetime.Transient.

[ValidateOnStartup]

Register a hosted service that validates an instance of the model when the application starts. Useful for validating configuration objects.

[ValidateOnStartup]
public class AppSettingsValidator : AbstractValidator<AppSettings>
{
    public AppSettingsValidator()
    {
        RuleFor(x => x.ConnectionString).NotEmpty();
        RuleFor(x => x.ApiKey).MinimumLength(32);
    }
}

AppSettings must be registered in DI (e.g. via services.AddSingleton(appSettings)). If the instance is not found, startup validation is silently skipped.

If validation fails at startup, an InvalidOperationException is thrown — your app will not start.


Minimal API Integration

WithValidation<T>() attaches a validation endpoint filter that automatically returns 400 ValidationProblem for invalid requests:

app.MapPost("/orders", (Order order) => Results.Ok())
   .WithValidation<Order>();

The generated ValidationFilter<T> resolves IValidator<T> from DI, validates the first matching argument, and returns RFC 7807-compliant validation errors.

Requires .NET 7 or later.


How It Works

At build time, the generator:

  1. Scans all type declarations with a base list
  2. Walks the inheritance chain looking for FluentValidation.AbstractValidator<T>
  3. Skips abstract classes and [SkipValidator]-decorated types
  4. Emits AddValidators() with the correct AddScoped / AddSingleton / AddTransient calls
  5. Emits ValidationFilter<T> and ValidatorStartupService<T> helpers as needed

No reflection. No assembly scanning. No runtime cost.


Diagnostics

Code Severity Description
AV001 Warning Multiple validators found for the same model type. Only the first is registered.
AV002 Warning AutoValidate attribute on a class that does not inherit AbstractValidator<T>.

Comparison with Assembly Scanning

Feature AddValidatorsFromAssembly() AutoValidate.Generator
Discovery Runtime reflection Compile-time
Registration overhead Assembly scan on startup Zero
AOT / NativeAOT compatible
IDE navigation ✅ (generated code is inspectable)
Startup validation Manual [ValidateOnStartup]

License

MIT © Justin Bannister

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

  • .NETStandard 2.0

    • 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.

Version Downloads Last Updated
1.0.0 108 6/25/2026

1.0.0: Initial release. Convention-based validator discovery, [SkipValidator], [ValidatorLifetime], [ValidateOnStartup], AddValidators(), WithValidation<T>() Minimal API helper.