A validation library for .NET that delivers an high-performance and memory prudence by using lambda-based and strongly-typed rules.
$ dotnet add package FlatValidatorThe FlatValidator is a validation library for .NET that delivers an high performance and memory prudence by using lambda-based and strongly-typed rules.
In general, there are two simple ways to validate custom data with the FlatValidator.
You can define validation rules in your code to validate object locally.
var model = new Model(Email: "email", BirthDate: DateTime.Now, Rate: -100);
// synchronous version
var result = FlatValidator.Validate(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(m => m.Rate < 0, "Negative Rate", m => m.Rate);
v.WarningIf(m => m.BirthDate.AddYears(10) >= DateTime.Now,
"Age looks like incorrect", m => m.BirthDate);
});
if (!result)
{
// ToDictionary() => Dictionary<PropertyName, ErrorMessage[]>
return TypedResults.ValidationProblem(result.ToDictionary())
}
// or asynchronous version
var result = await FlatValidator.ValidateAsync(model, v =>
{
v.ErrorIf(async m => await userService.IsUserExistAsync(m.Email),
"User already registered", m => m.Email);
// the same without `async/await`
v.ErrorIf(m => userService.IsUserExistAsync(m.Email),
m => $"Email {m.Email} already registered",
m => m.Email);
});
FlatValidator classAnother way is to inherit the
FlatValidatorto define custom rules in the constructor. Also you can pass dependencies into constructor to get additional functionality inside of the validation rules.
public record UserModel(string Forename, string Surname, ....);
public class UserValidator: FlatValidator<UserModel>
{
public UserValidator(ILogger logger, IPostalService postalService)
{
logger.LogInfo("Validating...");
ErrorIf(m => m.Forename.IsEmpty(), "Forename can not be empty.", m => m.Forename);
ErrorIf(m => m.Surname.IsEmpty(), "Surname can not be empty.", m => m.Surname);
// you can define one or more groups of preconditions
If(m => m.ShipmentAddress.NotEmpty(), @then: m =>
{
ValidIf(async m => await postalService.AddressExistsAsync(m.Address),
"Postal address not found.", m => m.Address);
WarningIf(m => !m.Phone.IsPhone(), "No contact phone.");
},
@else: m => // optionally
{
ValidIf(m => m.Phone.IsPhone(), "invalid phone number.", m => m.Phone);
});
}
}
Now lets validate some object with it
// now let's validate
var validator = new UserValidator();
var result = validator.Validate(new UserModel(...)); // synchronous call of your UserValidator
var result = await validator.ValidateAsync(customer, cancellationToken); // the same asynchronously
if (!result)
{
var errors = result.Errors; // result.Errors is a List<PropertyName, error, Tag>
var dict = result.ToDictionary(); // dict is a Dictionary<PropertyName, ErrorMessage[]>
}
// Inspect any validation failures.
bool success = results.IsValid;
List<ValidationFailure> failures = results.Errors;
TIP - The package
FlatValidator.DependencyInjectionhelps you to register all inherited validators in the ServiceCollection automatically.
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.