FlatValidator.DependencyInjection
2.3.0
See the version list below for details.
dotnet add package FlatValidator.DependencyInjection --version 2.3.0
NuGet\Install-Package FlatValidator.DependencyInjection -Version 2.3.0
<PackageReference Include="FlatValidator.DependencyInjection" Version="2.3.0" />
paket add FlatValidator.DependencyInjection --version 2.3.0
#r "nuget: FlatValidator.DependencyInjection, 2.3.0"
// Install FlatValidator.DependencyInjection as a Cake Addin #addin nuget:?package=FlatValidator.DependencyInjection&version=2.3.0 // Install FlatValidator.DependencyInjection as a Cake Tool #tool nuget:?package=FlatValidator.DependencyInjection&version=2.3.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
If(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.DependencyInjection
package 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 | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. 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. |
-
net6.0
- FlatValidator (>= 2.3.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 6.0.0)
-
net7.0
- FlatValidator (>= 2.3.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 6.0.0)
-
net8.0
- FlatValidator (>= 2.3.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 6.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Please read the guide at https://github.com/belset/FlatValidator/blob/main/CHANGELOG.md