FlatValidator.DependencyInjection
3.0.0
dotnet add package FlatValidator.DependencyInjection --version 3.0.0
NuGet\Install-Package FlatValidator.DependencyInjection -Version 3.0.0
<PackageReference Include="FlatValidator.DependencyInjection" Version="3.0.0" />
<PackageVersion Include="FlatValidator.DependencyInjection" Version="3.0.0" />
<PackageReference Include="FlatValidator.DependencyInjection" />
paket add FlatValidator.DependencyInjection --version 3.0.0
#r "nuget: FlatValidator.DependencyInjection, 3.0.0"
#:package FlatValidator.DependencyInjection@3.0.0
#addin nuget:?package=FlatValidator.DependencyInjection&version=3.0.0
#tool nuget:?package=FlatValidator.DependencyInjection&version=3.0.0
The FlatValidator is a validation library for .NET that delivers an high-performance and memory prudence by using lambda-based and strongly-typed rules.
The FlatValidator.DependencyInjection package extends the FlatValidator package to register all custom inherited validators in the IServiceCollection (Microsoft.Extensions.Dependencyinjection.Abstractions) automatically.
public static IServiceCollection AddCustomValidators(this IServiceCollection services)
{
services.AddFlatValidatorsFromAssembly(Assembly.GetExecutingAssembly());
return services;
}
Quick examples
1. Inheritance of the FlatValidator class
public record UserModel(string Phone, string ShipmentAddress, string PostalCode);
public class UserValidator: FlatValidator<UserModel>
{
public UserValidator(IPostalService postalService)
{
ErrorIf(m => m.Phone.IsPhoneNumber(), "Invalid phone number.", m => m.Phone);
// define one or more groups for preconditions
When(m => m.ShipmentAddress.NotEmpty(), @then: m =>
{
ValidIf(m => postalService.AddressExistsAsync(m.ShipmentAddress, m.PostalCode),
"Invalid postal address and/or postal code.",
m => m.ShipmentAddress, m => m.PostalCode);
});
}
}
// .... we want a synchronous version to validate here!
var result = new UserValidator().Validate(new UserModel(...));
// possibility to inspect occured validation failures
bool success = result.IsValid;
var errors = result.Errors;
var warnings = result.Warnings;
2. Using FlatValidator in inline mode:
var model = new Model(Email: "email", BirthDate: DateTime.Now, Rate: -100);
// .... now use an asynchronous version!
var result = await FlatValidator.ValidateAsync(model, v =>
{
// IsEmail() is one of funcs for typical data formats like Phone, Url, CreditCard, etc.
v.ValidIf(m => m.Email.IsEmail(), "Invalid email", m => m.Email);
v.ErrorIf(async m => await userService.IsUserExistAsync(m.Email),
m => $"Email {m.Email} already registered", m => m.Email);
});
if (!result)
{
// ToDictionary() => Dictionary<PropertyName, ErrorMessage[]>
var dict = result.ToDictionary();
}
Note - You don't need to install the
FlatValidator.DependencyInjectionpackage for inline mode usage.
Release Notes and Change Log
Release notes can be found on GitHub.
Supporting the project
The FlatValidator is developed and supported by @belset for free in spare time, so that financial help keeps the projects to be going successfully.
You can sponsor the project via Buy me a coffee.
| 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 is compatible. 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 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
- FlatValidator (>= 3.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.1)
-
net8.0
- FlatValidator (>= 3.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.1)
-
net9.0
- FlatValidator (>= 3.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.1)
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 |
|---|---|---|
| 3.0.0 | 4,593 | 11/13/2025 |
| 2.6.3 | 21,681 | 10/15/2025 |
| 2.6.2 | 58,965 | 5/28/2025 |
| 2.6.0 | 39,064 | 2/3/2025 |
| 2.5.0 | 21,998 | 11/19/2024 |
| 2.4.0 | 6,232 | 9/20/2024 |
| 2.3.0 | 10,740 | 5/7/2024 |
| 2.2.0 | 2,446 | 3/17/2024 |
| 2.1.1 | 417 | 3/16/2024 |
| 2.1.0 | 310 | 2/27/2024 |
| 2.0.0 | 1,508 | 2/1/2024 |
| 1.0.0 | 184 | 1/27/2024 |
| 1.0.0-rc | 154 | 1/25/2024 |
Please read the guide at https://github.com/belset/FlatValidator/blob/main/CHANGELOG.md