FluentAnnotationsValidator 2.0.0-preview.2.2

This is a prerelease version of FluentAnnotationsValidator.
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package FluentAnnotationsValidator --version 2.0.0-preview.2.2
                    
NuGet\Install-Package FluentAnnotationsValidator -Version 2.0.0-preview.2.2
                    
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="FluentAnnotationsValidator" Version="2.0.0-preview.2.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="FluentAnnotationsValidator" Version="2.0.0-preview.2.2" />
                    
Directory.Packages.props
<PackageReference Include="FluentAnnotationsValidator" />
                    
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 FluentAnnotationsValidator --version 2.0.0-preview.2.2
                    
#r "nuget: FluentAnnotationsValidator, 2.0.0-preview.2.2"
                    
#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 FluentAnnotationsValidator@2.0.0-preview.2.2
                    
#: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=FluentAnnotationsValidator&version=2.0.0-preview.2.2&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=FluentAnnotationsValidator&version=2.0.0-preview.2.2&prerelease
                    
Install as a Cake Tool

FluentAnnotationsValidator

A fluent, type-safe validation engine for .NET that transforms [ValidationAttribute] annotations into runtime validation logic. Designed for ergonomic configuration, conditional logic, and culture-aware localization.


Version 2.0.0-preview.2: Introducing a Fluent API

This release marks a significant architectural shift by introducing a new, more expressive fluent API for defining validation rules. This new API provides greater flexibility for complex and conditional validation flows compared to the previous purely attribute-based approach.


New API and Behavior
  • Rule(...): This method is now overloaded. It takes a property expression and an optional RuleDefinitionBehavior enum. By default, it replaces all previously registered rules for that property before adding the new one. The method returns a reference to the ValidationTypeConfigurator<T> instance, allowing you to configure the rule further.

  • RuleFor(...): This new method takes a property expression and returns a reference to a new, property-specific builder (IValidationRuleBuilder<T, TProp>). It does not override existing rules for the property. If you want to override pre-registered rules, call the RemoveRulesFor<TProp>(Expression<Func<T, TProp>>) method. This new builder enables powerful, type-safe chaining of validation methods and conditional logic.


New Features
  • Conditional Validation (When/Otherwise): The RuleFor(...) builder introduces a robust way to implement conditional logic.

    • When(condition, configureRules): This method allows you to group multiple validation rules that will only be evaluated if the provided condition is true.
    • Otherwise(configureRules): This method provides a corresponding set of rules that will be evaluated if the When condition is false, creating a clear if/else validation flow.
  • Custom Validation (Must): The new Must(predicate) method is a key addition to the IValidationRuleBuilder<T, TProp>. It allows developers to define custom validation logic using a predicate (Func<TProp, bool>) that is executed on the property's value. This method is fully integrated into the fluent chaining, enabling complex validation rules that are not possible with standard attributes.


🌟 What's New in v2.0.0-preview1

FluentAnnotationsValidator v2.0.0-preview1 is a fresh rewrite. All legacy APIs from v1.x have been removed.

  • Multi-attribute validation per property
  • DSL-based configuration via ValidationConfigurator
  • Conditional rules with .When(...), .Localized(...), .UseFallbackMessage(...)
  • Convention-based registration from scanned assemblies
  • Pluggable IValidationMessageResolver
  • Scoped culture + resource binding per type
  • Legacy support removed — clean slate architecture

To use the legacy version, pin to v1.2.2.


Quickstart

Basic Setup

Using AddFluentAnnotations():

using FluentAnnotationsValidator.Extensions;

services.AddFluentAnnotations();

Advanced Setup

Using either:

  1. AddFluentAnnotationsValidators(...):
services.AddFluentAnnotationsValidators(typeof(LoginDto))
    .UseFluentAnnotations()
    .For<LoginDto>()
        .WithCulture(CultureInfo.GetCultureInfo("fr-FR"))
        .WithValidationResource<ValidationMessages>()
    .Build();
  1. AddFluentAnnotations(...) with common behavior options configuration:
services.AddFluentAnnotations(
    configureBehavior: options =>
    {
        // common culture and resource type for all validation attributes
        options.CommonCulture = CultureInfo.GetCultureInfo("fr-FR");
        options.CommonResourceType = typeof(ValidationMessages);
    }
);
  1. AddFluentAnnotations(...) with scoped and common culture and resource types:
services.AddFluentAnnotations(
    builder =>
        // Conditional Localization rule for German 
        // culture and resource type scoped to LoginDto
        builder.For<LoginDto>()
            .When(x => x.LangCode == 'DE')
            .WithCulture(CultureInfo.GetCultureInfo("de-DE"))
            .WithValidationResource<AuthenticationMessages>()
        .Build(),
    configureBehavior: options =>
    {
        // common French culture and resource type for all validation rules
        options.CommonCulture = CultureInfo.GetCultureInfo("fr-FR");
        options.CommonResourceType = typeof(ValidationMessages);
    }
);

Installation

dotnet add package FluentAnnotationsValidator --version 2.0.0-preview.2.2

Key Concepts

Concept Description
ValidationBehaviorOptions Registry of validation rules discovered via scanning or configuration
FluentAnnotationsBuilder Configuration anchor: links DI services + options
ValidationConfigurator Fluent DSL to configure conditional logic, culture, and resource resolution
IValidationMessageResolver Pluggable fallback resolution for localized messages
DataAnnotationsValidator<T> Runtime validator that hydrates validation rules from metadata and rules registry

Test Coverage

  • [Required], [EmailAddress], [MinLength], [Range], [StringLength]
  • .resx and static resource support
  • ✅ Record constructor annotations
  • ✅ Upfront rule hydration + conditional overrides

Learn More


Contribute

Help shape validation ergonomics for developers worldwide. Open to extensions, diagnostics, and new DSL patterns — bring your ideas!

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  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

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
2.0.0-preview1 401 7/25/2025
2.0.0-preview.2.2 97 8/31/2025
1.2.2 547 7/23/2025
1.2.1 518 7/22/2025
1.2.0 533 7/22/2025
1.1.0 477 7/21/2025