AnnotatedResult 1.0.0-preview.13

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

// Install AnnotatedResult as a Cake Tool
#tool nuget:?package=AnnotatedResult&version=1.0.0-preview.13&prerelease                

AnnotatedResult

Nuget Nuget GitHub

AnnotatedResult is a lightweight .NET model validation library utilizing System.ComponentModel.DataAnnotations.

NuGet Package

dotnet add package AnnotatedResult --version 1.0.0-preview.12

Overview

Validation

public class Request
{
    [Required]
    [RegularExpression(RegexPattern.Username)]
    public string Username { get; init; }

    [EmailAddress]
    public string Email { get; init; }

    [Required]
    [RegularExpression(RegexPattern.StrongPassword)]
    public string Password { get; init; }
}

Request is a class with Data Annotation attribute assigned for each properties. AnnotatedResult make it so it's easy to validate.

    var request = new Request();
    bool isValid = request.TryValidate(out Error[] errors);

TryValidate is an extension method use to validation any instance of object. If the object has Data Annotation attribute assigned to either field of property it will perform the validation process and return a boolean and an out parameter of type Error[].

    if(!isValid)
    {
        Result<Request> result = Result<Request>.Invalid(errors);
        return result;
    }

    Result<Request> result = Result<Request>.Ok(request);
    return result;

Result<T> is a generic wrapper class that encapsulate Data Annotation validation result. The example above it hold the generic type of Request. Result<T> can be modified whether the is a success or an error--or a specified error such as Unauthorized or NotFound.

Result<T> with 200 status code or Ok status return the validated object in question where as error (specified or otherwise) return list of errors.

    var ok           = Result<Request>.Ok(request);

    var error        = Result<Request>.Error(errors);
    var invalid      = Result<Request>.Invalid(errors);
    var unauthorized = Result<Request>.Unauthorized(errors);
    var forbidden    = Result<Request>.Forbidden(errors);
    var conflict     = Result<Request>.Conflict(errors);
    var notFound     = Result<Request>.NotFound(errors);

Usage

The library provides a convenient way to handle the validation results using the Result<T> class. Here are some common patterns:

Match Success and Failure

    result.Match(
        () => Console.WriteLine("Operation successful!"),
        (statusAndErrors) => Console.WriteLine($"Operation failed with status {statusAndErrors.status}. Errors: {statusAndErrors.errors.Count}")
    );

Match Success and Return Value

    string resultValue = result.Match(
        (value) => value,
        (statusAndErrors) => "Default Value"
    );

Add Metadata

    result.AddMetadata(new Dictionary<string, object>
    {
        { "Key1", "Value1" },
        { "Key2", 42 }
    });

Inherit Result

    Result<DerivedType> derivedResult = Result<DerivedType>.Inherit(result);
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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  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.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on AnnotatedResult:

Package Downloads
AnnotatedResult.AspNetCore

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0-rc.5 81 2/26/2024
1.0.0-rc.4 88 1/24/2024
1.0.0-rc.3 56 1/24/2024
1.0.0-rc.2 61 1/19/2024
1.0.0-rc.1 71 1/7/2024
1.0.0-preview.13 85 12/31/2023
1.0.0-preview.12 82 12/25/2023
1.0.0-preview.11 93 12/6/2023
1.0.0-preview.10 75 12/3/2023
1.0.0-preview.9 72 12/1/2023
1.0.0-preview.8 116 9/5/2022
1.0.0-preview.7 103 9/4/2022
1.0.0-preview.6 105 9/4/2022
1.0.0-preview.5 104 8/30/2022
1.0.0-preview.4 102 8/28/2022
1.0.0-preview.3 102 8/28/2022
1.0.0-preview.2 107 8/26/2022
1.0.0-preview.1 113 8/24/2022