AoK.FluentValidation 1.9.0

dotnet add package AoK.FluentValidation --version 1.9.0
NuGet\Install-Package AoK.FluentValidation -Version 1.9.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AoK.FluentValidation" Version="1.9.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AoK.FluentValidation --version 1.9.0
#r "nuget: AoK.FluentValidation, 1.9.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install AoK.FluentValidation as a Cake Addin
#addin nuget:?package=AoK.FluentValidation&version=1.9.0

// Install AoK.FluentValidation as a Cake Tool
#tool nuget:?package=AoK.FluentValidation&version=1.9.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

  1. Validator throws InvalidOperationException when validator.Error is called before validator.IsValid()
  2. Passing null validation will result in validation always passing
Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .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.

Version Downloads Last updated
1.9.0 170 1/1/2024
1.8.0 90 1/1/2024
1.7.0 106 1/1/2024
1.6.0 111 1/1/2024
1.5.0 109 12/27/2023
1.4.0 102 12/27/2023
1.3.0 92 12/27/2023
1.2.0 110 12/27/2023
1.1.0 93 12/27/2023
1.0.0 100 12/27/2023

Minor optimizations.