Oneiro.FSharp.Validations 2.0.0

dotnet add package Oneiro.FSharp.Validations --version 2.0.0
                    
NuGet\Install-Package Oneiro.FSharp.Validations -Version 2.0.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="Oneiro.FSharp.Validations" Version="2.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Oneiro.FSharp.Validations" Version="2.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Oneiro.FSharp.Validations" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Oneiro.FSharp.Validations --version 2.0.0
                    
#r "nuget: Oneiro.FSharp.Validations, 2.0.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.
#:package Oneiro.FSharp.Validations@2.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Oneiro.FSharp.Validations&version=2.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Oneiro.FSharp.Validations&version=2.0.0
                    
Install as a Cake Tool

FSharp.Validations

FSharp Validations CI NuGet Version

FSharp Validations is a validation library designed with a functional first approach.

The library supports:

  • The ability to create rules for properties using the RuleFor<'a, 'b> function
  • The ability to apply a list of rules to a propery using Rule<'a>
  • the ability to reuse validators using ruleSetFor<'a, 'b>
  • The creation of RuleSets<'a> -> 'a ValidationResult

The rules can then be evaluated at any time thanks to the magic of currying.

open FSharp.Validations

type Foo = { Bar: string }

let validateFoo =
    ruleSet [
        ruleFor <@ _.Bar @> [
            rule (fun x -> x.Length > 0) (Some "Length must be greater than 0")
            rule (fun x -> x.Length < 10) (Some "Length must be less than 10") ] ] 

let result = validateFoo { Bar = "Hello, World!" }

// result is Failure (map [("Bar", ["Value must be less than 10"])])

Reusing Rule Sets

Rule sets can be reused by creating a RuleSet<'a> and applying it to a property using the ruleSetFor<'a, 'b> function.

type Bar = { Value: string }
type Foo = { Bar: Bar }

let validateBar =
    ruleSet [
        ruleFor <@ _.Value @> [
            rule (fun x -> x.Length > 0) (Some "Length must be greater than 0")
            rule (fun x -> x.Length < 10) (Some "Length must be less than 10") ] ]

let validateFoo =
    ruleSet [
        ruleSetFor <@ _.Bar @> validateBar ]

Any resulting errors for Bar will have a key of Bar.{propertyName}.

ValidationResult<'a>

A Success represents the entity that has passed validations.

A Failure represents a Map<string, string list> where the key is the property that failed and value is a collection of all the messages attached to the failure upon validation.

let result = validateFoo { bar = "Hello, World!" }

match result with
| Success value -> printfn "%A" value
| Failure errors -> errors |> Map.iter (printfn "%s: %A")

Dependency Injection

A function is provided to convert any 'a -> 'a ValidationResult to an 'a IValidator using the provided toValidator function.

The resulting 'a IValidator can be used for Dependency Injection.

open Microsoft.Extensions.DependencyInjection

type Foo = { Bar: string }

let validateFoo =
    ruleSet<Foo> [ ruleFor <@ _.Bar @> [ notEmpty ] ]

let fooValidator = validateFoo |> toValidator

let result = fooValidator.Validate({ Bar = "Hello, World!" })

// Add to Dependency Injection

let services = ServiceCollection() :> IServiceCollection
services.AddScoped<Foo IValidator>(fun _ -> fooValidator) |> ignore

License, Copywright, Etc.

FSharp.Validations is subject to copywright @ 2025 Oneirosoft and other contributors under the MIT License.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
2.0.0 121 2/8/2025