FluentAnnotationsValidator 2.0.0-preview.2.3
See the version list below for details.
dotnet add package FluentAnnotationsValidator --version 2.0.0-preview.2.3
NuGet\Install-Package FluentAnnotationsValidator -Version 2.0.0-preview.2.3
<PackageReference Include="FluentAnnotationsValidator" Version="2.0.0-preview.2.3" />
<PackageVersion Include="FluentAnnotationsValidator" Version="2.0.0-preview.2.3" />
<PackageReference Include="FluentAnnotationsValidator" />
paket add FluentAnnotationsValidator --version 2.0.0-preview.2.3
#r "nuget: FluentAnnotationsValidator, 2.0.0-preview.2.3"
#:package FluentAnnotationsValidator@2.0.0-preview.2.3
#addin nuget:?package=FluentAnnotationsValidator&version=2.0.0-preview.2.3&prerelease
#tool nuget:?package=FluentAnnotationsValidator&version=2.0.0-preview.2.3&prerelease
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 optionalRuleDefinitionBehaviorenum. By default, it replaces all previously registered rules for that property before adding the new one. The method returns a reference to theValidationTypeConfigurator<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 theRemoveRulesFor<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): TheRuleFor(...)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 providedconditionis true.Otherwise(configureRules): This method provides a corresponding set of rules that will be evaluated if theWhencondition is false, creating a clearif/elsevalidation flow.
Custom Validation (
Must): The newMust(predicate)method is a key addition to theIValidationRuleBuilder<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:
AddFluentAnnotationsValidators(...):
services.AddFluentAnnotationsValidators(typeof(LoginDto))
.UseFluentAnnotations()
.For<LoginDto>()
.WithCulture(CultureInfo.GetCultureInfo("fr-FR"))
.WithValidationResource<ValidationMessages>()
.Build();
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);
}
);
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.3
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] - ✅
.resxand 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 | Versions 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. |
-
net8.0
- Microsoft.Extensions.Localization (>= 8.0.2)
- Microsoft.Extensions.Localization.Abstractions (>= 8.0.2)
- Microsoft.Extensions.Logging (>= 8.0.1)
- Microsoft.Extensions.Options (>= 8.0.2)
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-rc.1.0.0 | 72 | 10/4/2025 |
| 2.0.0-preview1 | 447 | 7/25/2025 |
| 2.0.0-preview.2.3 | 97 | 9/26/2025 |
| 2.0.0-preview.2.2 | 122 | 8/31/2025 |
| 1.2.2 | 605 | 7/23/2025 |
| 1.2.1 | 592 | 7/22/2025 |
| 1.2.0 | 596 | 7/22/2025 |
| 1.1.0 | 526 | 7/21/2025 |