Skip to content

Latest commit

 

History

History
61 lines (48 loc) · 1.97 KB

README.md

File metadata and controls

61 lines (48 loc) · 1.97 KB

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 a set of utilities to help with this, integrating with the wonderful FluentValidation library.

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 kitchen sink example below demonstrates the functionality delivered from this slim library built on top of Danom.

using Danom;
using Danom.Validation;
using FluentValidation;

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

public class AttendeeValidator
    : AbstractValidator<Attendee>
{
    public AttendeeValidator()
    {
        RuleFor(x => x.Name).NotEmpty();
        RuleFor(x => x.Age).GreaterThan(0);
        RuleFor(x => x.Email).Required(x => x.EmailAddress());
        RuleFor(x => x.AlternateEmail).Optional(x => x.EmailAddress());
    }
}

var input =
    new Attendee(
        Name: "John Doe",
        Age: 30,
        Email: Option<string>.Some("[email protected]"),
        AlternateEmail: Option<string>.None());

var result =
    ValidationResult<Attendee>
        .From<AttendeeValidator>(input);

result.Match(
    x => Console.WriteLine("Input is valid: {0}", x),
    e => Console.WriteLine("Input is invalid: {0}", e));