FlatValidator.DependencyInjection
1.0.0-rc
See the version list below for details.
dotnet add package FlatValidator.DependencyInjection --version 1.0.0-rc
NuGet\Install-Package FlatValidator.DependencyInjection -Version 1.0.0-rc
<PackageReference Include="FlatValidator.DependencyInjection" Version="1.0.0-rc" />
paket add FlatValidator.DependencyInjection --version 1.0.0-rc
#r "nuget: FlatValidator.DependencyInjection, 1.0.0-rc"
// Install FlatValidator.DependencyInjection as a Cake Addin #addin nuget:?package=FlatValidator.DependencyInjection&version=1.0.0-rc&prerelease // Install FlatValidator.DependencyInjection as a Cake Tool #tool nuget:?package=FlatValidator.DependencyInjection&version=1.0.0-rc&prerelease
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
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
Group(m => m.ShipmentAddress.NotEmpty(), m =>
{
ValidIf(m => postalService.AddressExistsAsync(m.ShipmentAddress, m.PostalCode),
"Invalid postal address and/or postal code.",
m => m.ShipmentAddress, m => m.PostalCode);
});
}
}
// .... we want to use synchronous version to validate here!
var result = new UserValidator().Validate(new UserModel(...));
2. Usage of the FlatValidator
in inline mode:
var model = new Model(Email: "email", BirthDate: DateTime.Now, Rate: -100);
// use 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);
});
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
If you like my activities, it may be great to give me a ⭐ and/or share this link with friends 🤗
The FlatValidator
is developed and supported by @belset for free in spare time, so that financial help keeps the projects to be going successfully.
<div>
<a href="https://www.buymeacoffee.com/belset" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/v2/default-yellow.png" width="150" /></a>
</div>
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. 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. |
-
net6.0
- FlatValidator (>= 1.0.0-rc)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 6.0.0)
-
net7.0
- FlatValidator (>= 1.0.0-rc)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 6.0.0)
-
net8.0
- FlatValidator (>= 1.0.0-rc)
- 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