LfrlAnvil.Validation 0.2.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package LfrlAnvil.Validation --version 0.2.0                
NuGet\Install-Package LfrlAnvil.Validation -Version 0.2.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="LfrlAnvil.Validation" Version="0.2.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add LfrlAnvil.Validation --version 0.2.0                
#r "nuget: LfrlAnvil.Validation, 0.2.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 LfrlAnvil.Validation as a Cake Addin
#addin nuget:?package=LfrlAnvil.Validation&version=0.2.0

// Install LfrlAnvil.Validation as a Cake Tool
#tool nuget:?package=LfrlAnvil.Validation&version=0.2.0                

(root) NuGet Badge

LfrlAnvil.Validation

This project contains definitions of various composable validators, as well as validation message formatters.

Documentation

Technical documentation can be found here.

Examples

Following are examples of a few chosen formattable validators:

// validator that always passes
var a = FormattableValidators<string>.Pass<int>();

// validator that always fails, with a formattable 'GenericFailure' message
var b = FormattableValidators<string>.Fail<int>( "GenericFailure" );

// validator that requires a string value to not be empty
var c = FormattableValidators<string>.NotEmpty( "EmptyText" );

// validator that requires a string value to match the given regular expression
var d = FormattableValidators<string>.Match( new Regex( ... ), "TextNotMatched" );

// validator that requires a nullable int value to not be null
var e = FormattableValidators<string>.NotNull<int?>( "NullValue" );

// validator that requires an int value to be greater than 42
var f = FormattableValidators<string>.GreaterThan( 42, ValidationMessage.Create( "ValueTooSmall", 42 ) );

// validator that chooses which underlying validator to use based on the given 'value >= 0' condition
// when an int value is greater than or equal to 0, then the 'f' validator will be used
// otherwise, the 'a' validator will be used
// that means that this validator requires, that an int value must be greater than 42 or be negative
var g = Validators<ValidationMessage<string>>.Conditional( value => value >= 0, f, a );

// validator that requires, that all underlying validators pass
// this means that this validator requires, that a string value is not empty
// and matches the given regular expression
// all errors from all underlying validators are included in validation result
var h = Validators<ValidationMessage<string>>.All( c, d );

// validators that requires, that at least one underlying validator passes
// this means that this validator requires, that a string value is not empty
// or matches the given regular expression
// all errors from all underlying validators are included in validation result,
// unless at least one of them passes, which will cause all errors to be discarded
var i = Validators<ValidationMessage<string>>.Any( c, d );

// a string value validator that uses an underlying validator for int values,
// which requires, that a string value's Length is greater than 42
var j = f.For( static (string x) => x.Length );

// validation message formatter that can be used to convert validation messages to strings
// an implementation of such a formatter can e.g. use a localization service,
// capable of translating message codes to messages in specific languages
IValidationMessageFormatter<string> formatter = ...;

// example of a formatted validator
var k = h.Format( formatter );

It's also possible to use a type different than string, as a type of validation message's resource. Following is an example of an enum that may serve such a purpose:

// validation message resource type definition as an enum
public enum ValidationResources
{
    GenericFailure,
    // other resource values
}

// example of a validator that uses 'ValidationResources' as its validation message's resource type
var validator = FormattableValidators<ValidationResources>.Fail<int>( ValidationResources.GenericFailure );
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on LfrlAnvil.Validation:

Package Downloads
LfrlAnvil.Reactive.State

This project contains a few functionalities related to state management.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.2.1 134 6/16/2024
0.2.0 118 6/16/2024
0.1.1 122 5/29/2024
0.1.0 124 5/26/2024