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
<PackageReference Include="Oneiro.FSharp.Validations" Version="2.0.0" />
<PackageVersion Include="Oneiro.FSharp.Validations" Version="2.0.0" />
<PackageReference Include="Oneiro.FSharp.Validations" />
paket add Oneiro.FSharp.Validations --version 2.0.0
#r "nuget: Oneiro.FSharp.Validations, 2.0.0"
#:package Oneiro.FSharp.Validations@2.0.0
#addin nuget:?package=Oneiro.FSharp.Validations&version=2.0.0
#tool nuget:?package=Oneiro.FSharp.Validations&version=2.0.0
FSharp.Validations
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 | Versions 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. |
-
net9.0
- FSharp.Core (>= 9.0.101)
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 |