AoK.FluentValidation
1.7.0
See the version list below for details.
dotnet add package AoK.FluentValidation --version 1.7.0
NuGet\Install-Package AoK.FluentValidation -Version 1.7.0
<PackageReference Include="AoK.FluentValidation" Version="1.7.0" />
paket add AoK.FluentValidation --version 1.7.0
#r "nuget: AoK.FluentValidation, 1.7.0"
// Install AoK.FluentValidation as a Cake Addin #addin nuget:?package=AoK.FluentValidation&version=1.7.0 // Install AoK.FluentValidation as a Cake Tool #tool nuget:?package=AoK.FluentValidation&version=1.7.0
About
AoK.FluentValidation addresses the issue of high cyclomatic complexity that arises from validations (parameters or otherwise) that are peformed as fail-fast before performing the function. In case of parameter validations, the cyclomatic complexity would become minimally (n + 1), where n is the number of parameters to be validated. AoK.FluentValidation, apart from helping you to write validation code fluently, reduces the complexity of such code to 2 irrespective of the number of parameters to be validated.
Usage
// Conventional
void ValidateParameters(SomeClass obj, int percent, string message)
{
if(obj == null)
{
// print error
return;
}
if(percent < 0 || percent > 100) // Cyclomatic complexity >= 4
{
// print error
return;
}
if(string.IsNullorEmpty(message))
{
// print error
return;
}
}
// AoK.FluentValidation
void ValidateParameters(SomeClass obj, int percent, string message)
{
IValidator validator =
ValidatorBuilder
.WithValidation(() => obj != null)
.AndFailureMessage($"{nameof(obj)} cannot be null")
.AndWithValidation(() => percent >= 0 && percent <= 100)
.AndFailureMessage($"{nameof(percent)} shall be in range 0..100") // cyclomatic complexity <= 3
.AndWithValidation(() => !string.IsNullorEmpty(message))
.AndFailureMessage($"{nameof(message)} shall not be null or empty")
.Build();
// validation
if(!validator.IsValid())
{
// print validator.Error
return;
}
}
Remarks
- Validator throws InvalidOperationException when validator.Error is called before validator.IsValid()
- Passing null validation will result in validation always passing
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET Framework | net461 is compatible. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
-
.NETFramework 4.6.1
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Improved error handling and some minor optimizations. Please refer to Readme.md for more details.