MonkeyValidator 1.1.3

.NET 6.0
NuGet\Install-Package MonkeyValidator -Version 1.1.3
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.
dotnet add package MonkeyValidator --version 1.1.3
<PackageReference Include="MonkeyValidator" Version="1.1.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MonkeyValidator --version 1.1.3
#r "nuget: MonkeyValidator, 1.1.3"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install MonkeyValidator as a Cake Addin
#addin nuget:?package=MonkeyValidator&version=1.1.3

// Install MonkeyValidator as a Cake Tool
#tool nuget:?package=MonkeyValidator&version=1.1.3

Monkey validator ⇒ a Linq approach to validation

This library aims at making validation easy, fluent and versatile.

Types of Validation

On The Fly

Use this type of validation anywhere in your code. Just called the GetValidator() on any object and start chaining your validation.

on the fly

Reusable

Create an ad-hoc validator class inheriting from CustomMonkeyValidatorBase<T> and build your validator in the abstract SetupValidator() method.

Reusable

validators can be chained, and the validation will pickup all errors before throwing.

MultiChaining

Conditional Validation

You can use an if/else if/else logic for more complex scenarios

ConditionalValidationExample

Fail Fast

If you want to break the validation if a certain condition is met, you can use the FailFastIf() extension, which will throw immediatelly if the predicate returns false.

failfast

Custom On Fail

If you want to insert your own logic on validation failure, you can use the overload that takes an Action<List<string>> where List<string> are the validation errors.

155908647-ed83b6f7-e41c-4529-9191-fca65e934f8c

You can set the optional throwMonkeyException flag to true if you just want to add some logic before the default exception is thrown.

CustomResult

Custom Rules

For on the fly, one time only custom rules, you can simply chain a CustomRule() and pass in your predicate.

155909572-eb2a1cf9-5a9b-4c10-8dd0-9e8c1aef49f3

For rules you want to reuse, but are not specific enough to put on a Validator class, I'd recommend creating a static class to extend the MonkeyValidator<T> with your rule fragments:

155909683-61bad9ca-f50e-49dc-b4f8-92f702faef94

which you can then chain to your other validators.

CustomResult

Injectable Validation

Sometimes, validation can hinder testability. A complex validator can become rather troublesome for testers, especially for testing edge cases where the validation has little to no bearing to the section of code being tested.

We can circumvent the issue by creating an injectable service like so:

injectable validation

This has the added advantage that if we need to inject other services that are required to fulfil our validation, those are encapsulated in the IStringValidator, and thus not required when unit testing. The IStringValidator can then be reused to add all the validation methods related to strings.

Foreach Logic

Foreach

Product Versions
.NET net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • 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.1.3 93 3/3/2022
1.1.2 79 3/1/2022
1.1.1 81 3/1/2022
1.0.1 81 2/28/2022