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 SomeModel(Email: "email@email.com", BirthDate: DateTime.Now, Rate: -100);
// synchronous version
var result = FlatValidator.Validate(model, v =>
{
// IsEmail() is built-in func for typical data formats
// like Email, Phone, Url, CreditCard, Password, etc.
v.ValidIf(m => m.Email.IsEmail(),
m => $"Invalid email: {m.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);
});
// or asynchronous version
var result = await FlatValidator.ValidateAsync(model, v =>
{
v.ErrorIf(async m => await remoteApi.IsEmailBlockedAsync(m.Email),
"Email is in black list.", m => m.Email);
// the same without `async/await`
v.ErrorIf(m => remoteApi.IsEmailBlockedAsync(m.Email),
m => $"Email {m.Email} is in black list.",
m => m.Email);
});
// to check the validation result
if (!result)
{
// ToDictionary() => Dictionary<PropertyName, ErrorMessage[]>
return TypedResults.ValidationProblem(result.ToDictionary())
}
// possibility to inspect occured validation failures
bool success = result.IsValid;
var errors = result.Errors;
var warnings = result.Warnings;
Another 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() || m.Surname.IsEmpty(),
"Forename and Surname can not be empty.",
m => m.Forename, m => m.Surname);
// use 'If(...)' to control a validation flow
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 => // @else section is optional
{
ValidIf(m => m.Phone.IsPhone(), "Invalid phone number.", m => m.Phone);
});
}
}
Now lets validate some object with it
// create instance of the custom validator
var validator = new UserValidator();
// validate _asynchronously_ and get a result
var result = await validator.ValidateAsync(customer, cancellationToken);
// OR validate _synchronously_ and get a result
var result = validator.Validate(new UserModel(...));
if (!result) // check, is there any errors?
{
// ToDictionary() => Dictionary<PropertyName, ErrorMessage[]>
var dict = result.ToDictionary();
var errors = result.Errors;
var warnings = result.Warnings;
}TIP - The package
FlatValidator.DependencyInjectionhelps you to register all inherited validators in the ServiceCollection automatically.
Using MetaData can extend functionality and can help to return certain data beyond the validator:
var result = FlatValidator.Validate(model, v =>
{
v.MetaData["ValidationTime"] = DateTime.UtcNow.ToString();
// ....
});
// access to the MetaData value outside of the validation
return result.MetaData["ValidationTime"];ErrorIf(str => str.IsEmpty(), ... - ensure the string is empty.ValidIf(str => str.NotEmpty(), ... - ensure the string is not empty.ErrorIf(guid => guid.IsEmpty(), ... - ensure the GUID is empty.ValidIf(guid => guid.NotEmpty(), ... - ensure the GUID is not empty.ErrorIf(guid => guid.IsEmpty(), ... - ensure the GUID? is null or empty.ValidIf(guid => guid.NotEmpty(), ... - ensure the GUID? is not null and not empty.ValidIf(eml => eml.IsEmail(), ... - check the string contains an email.ValidIf(phnum => phnum.IsPhoneNumber(), ... - check the string contains a phone number.ValidIf(cardnum => cardnum.IsCreditCardNumber(), ... - check the string contains a credit card number.ValidIf(carddt => carddt.IsCreditCardExpiryDate(), ... - check the string contains an expiration date for credit card in format MM/yy. false.ValidIf(cvv => cvv.IsCreditCardCVV(), ... - check the string contains a CVV.ValidIf(uri => uri.IsAbsoluteUri(), ... - returns false if URI value:
c:\dir\file.file://c:/dir/file.http:\\host/path\file or file:\\\c:\path.ValidIf(str => str.IsPassword(), ... - check password occupancy rate; FlatValidatorFuncs.GetPasswordStrength(string? password) - calculates the cardinality of the minimal character sets necessary to brute force the password (roughly).PasswordStrength as one value of the VeryWeak, Weak, Medium, Strong, VeryStrong enum.FlatValidatorFuncs.GetPasswordStrength(string? password, out int score, out int maxScore) score - score for the password, it is always less than maxScore;maxScore - calculated max score that is possible for this password. PasswordStrength as one value of the VeryWeak, Weak, Medium, Strong, VeryStrong enum.FlatValidatorFuncs.GetShannonEntropy(string password) - this uses the Shannon entropy equation to estimate the average minimum number of bits needed to encode a string of symbols, based on the frequency of the symbols. double value that's Shannon entropy.ValidIf(str => str.AllCyrillic(), ... - true, if there are only Cyrillic symbols.ValidIf(str => str.HasCyrillic(), ... - true, if there is at least one Cyrillic symbol.ValidIf(str => str.AllCyrillicSupplement(), ... - true, if there are only Cyrillic symbols from Cyrillic Supplement that's a Unicode block containing Cyrillic letters for writing several minority languages, including Abkhaz, Kurdish, Komi, Mordvin, Aleut, Azerbaijani, and Jakovlev's Chuvash orthography.ValidIf(str => str.AllBasicLatin(), ... - true, if there are only Latin symbols.ValidIf(str => str.HasBasicLatin(), ... - true, if there is at least one Latin symbols.The error message for each validator can be formatted with checked data that may be filled in when the error message is constructed.
The ErrorId() and ValidIf() have two possibilities to return some error message:
ErrorIf(eml => eml.IsEmail(), "Invalid email.")ErrorIf(eml => eml.IsEmail(), eml => "Email {eml} is invalid.")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.