The FlatValidator.DependencyInjection package extends the FlatValidator package to register all custom inherited validators in the IServiceCollection (Microsoft.Extensions.Dependencyinjection.Abstractions) automatically. A validation library for .NET that provides high performance and memory efficiency with the usage of strongly-typed and lambda-based rules.
$ dotnet add package FlatValidator.DependencyInjectionThe 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;
}
FlatValidator classpublic 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;
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 can be found on GitHub.
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.