Danom.Validation 2.0.0-beta6

This is a prerelease version of Danom.Validation.
dotnet add package Danom.Validation --version 2.0.0-beta6
                    
NuGet\Install-Package Danom.Validation -Version 2.0.0-beta6
                    
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="Danom.Validation" Version="2.0.0-beta6" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Danom.Validation" Version="2.0.0-beta6" />
                    
Directory.Packages.props
<PackageReference Include="Danom.Validation" />
                    
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 Danom.Validation --version 2.0.0-beta6
                    
#r "nuget: Danom.Validation, 2.0.0-beta6"
                    
#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 Danom.Validation@2.0.0-beta6
                    
#: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=Danom.Validation&version=2.0.0-beta6&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Danom.Validation&version=2.0.0-beta6&prerelease
                    
Install as a Cake Tool

Danom.Validation

NuGet Version build

One of the places the Result type really shines is input validation. It's a natural step in most workflows to validate input data before processing it, and the Result type is a great way to handle this. The Danom.Validation library provides an API for defining validation rules and checking input data against those rules, returning a Result<T, ResultErrors> that contains either the validated data or an error message.

Getting Started

Install the Danom.Validation NuGet package:

PM>  Install-Package Danom.Validation

Or using the dotnet CLI

dotnet add package Danom.Validation

Example

The example below demonstrates the functionality delivered from this slim API built on top of Danom.

using Danom;
using Danom.Validation;

public record Attendee(
    string Name,
    int Age,
    string Phone,
    Option<string> Email,
    Option<string> AlternateEmail,
    IEnumerable<string> Interests);

public sealed class AttendeeValidator : BaseValidator<Attendee>
{
    public AttendeeValidator()
    {
        Rule("Name", x => x.Name,
            Check.String.IsNotEmpty);

        Rule("Age", x => x.Age,
            Check.IsGreaterThan(0));

        Rule("Phone", x => x.Phone,
            Check.String.IsE164);

        Rule("Email", x => x.Email,
            Check.Required(Check.String.IsEmailAddress));

        Rule("AlternateEmail", x => x.AlternateEmail,
            Check.Optional(Check.String.IsEmailAddress));

        Rule("Interests", x => x.Interests,
            Check.Enumerable.ForEach(Check.String.IsLengthOrGreaterThan(2)));
    }
}

Validate<Attendee>
    .Using<AttendeeValidator>(new(
        Name: "John Doe",
        Age: 30,
        Phone: "+14055551234",
        Email: Option<string>.Some("john@doe.com"),
        AlternateEmail: Option<string>.None(),
        Interests: ["C#", "ASP.NET"]))
    .Match(
        ok: x => Console.WriteLine("Input is valid: {0}", x),
        error: e => Console.WriteLine("Input is invalid: {0}", e));

// or, if you don't care about the error messages use Result.ToOption() to
// flatten the result to an Option<T>
Validate<Attendee>
    .Using<AttendeeValidator>(new(
        Name: "",
        Age: -1,
        Phone: "123",
        Email: Option<string>.NoneValue,
        AlternateEmail: Option<string>.Some("invalid_email"),
        Interests: ["a"]))
    .ToOption()
    .Match(
        some: x => Console.WriteLine("Input is valid: {0}", x),
        none: () => Console.WriteLine("Input is invalid"));

Find a bug?

There's an issue for that.

License

Licensed under MIT.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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.  net9.0 was computed.  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. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.1

NuGet packages (2)

Showing the top 2 NuGet packages that depend on Danom.Validation:

Package Downloads
Danom.Mvc

ASP.NET Core MVC and Razor Pages support for Danom.

Danom.MinimalApi

ASP.NET Core Minimal API support for Danom.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.0-beta6 144 8/8/2025
2.0.0-beta4 158 6/24/2025
2.0.0-beta2 224 5/16/2025
2.0.0-beta1 108 4/18/2025
1.2.0 152 12/6/2024
1.1.1 121 12/6/2024
1.0.0 159 11/20/2024
1.0.0-beta1 114 10/11/2024
1.0.0-alpha1 124 8/30/2024