CyberAndrii.Grpc.AspNetCore.Validation 1.0.2

This package has a SemVer 2.0.0 package version: 1.0.2+8a2b312.
dotnet add package CyberAndrii.Grpc.AspNetCore.Validation --version 1.0.2                
NuGet\Install-Package CyberAndrii.Grpc.AspNetCore.Validation -Version 1.0.2                
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="CyberAndrii.Grpc.AspNetCore.Validation" Version="1.0.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add CyberAndrii.Grpc.AspNetCore.Validation --version 1.0.2                
#r "nuget: CyberAndrii.Grpc.AspNetCore.Validation, 1.0.2"                
#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 CyberAndrii.Grpc.AspNetCore.Validation as a Cake Addin
#addin nuget:?package=CyberAndrii.Grpc.AspNetCore.Validation&version=1.0.2

// Install CyberAndrii.Grpc.AspNetCore.Validation as a Cake Tool
#tool nuget:?package=CyberAndrii.Grpc.AspNetCore.Validation&version=1.0.2                

CyberAndrii.Grpc.AspNetCore.Validation

GRPC request validator middleware with detailed error response for proper client side localization support.

Build and Publish Nuget

Setup

Create a new validator using FluentValidation library:

using FluentValidation;

public class HelloRequestValidator : AbstractValidator<HelloRequest>
{
    public HelloRequestValidator()
    {
        RuleFor(request => request.Name)
            .Length(3, 30);
    }
}

Configure response and register your validators:

builder.Services
    .AddGrpcValidationGlobally(options =>
    {
        options.IncludeErrorMessage = true;
        options.IncludeMessagePlaceholders = true;
        options.IncludeAttemptedValue = true;
        options.IncludeSeverity = true;
        options.IncludeErrorCode = true;
    })
    .AddValidator<HelloRequestValidator, HelloRequest>();

// If you want to use client side localization. Usefull with IncludeMessagePlaceholders enabled.
ValidatorOptions.Global.MessageFormatterFactory = () => new KeepPlaceholdersMessageFormatter();

If you are making requests from a browser or any JavaScript client, don't forget to setup grpc-web and CORS rules with the following headers:

.WithExposedHeaders("Grpc-Status", "Grpc-Message", "Grpc-Encoding", "Grpc-Accept-Encoding")
.WithExposedHeaders("validation-errors-text");

Retrieving validation errors

Errors come as a base64-encoded JSON object in the trailer response headers:

validation-errors-text: W3sicHJvcGVydHlOYW1lIjoiTmFtZSIsImVycm9yTWVzc2FnZSI6Ilx1MDAyN3tQcm9wZXJ0eU5hbWV9XHUwMDI3IG11c3QgYmUgYmV0d2VlbiB7TWluTGVuZ3RofSBhbmQge01heExlbmd0aH0gY2hhcmFjdGVycy4gWW91IGVudGVyZWQge1RvdGFsTGVuZ3RofSBjaGFyYWN0ZXJzLiIsInBsYWNlaG9sZGVycyI6eyJNaW5MZW5ndGgiOjMsIk1heExlbmd0aCI6MzAsIlRvdGFsTGVuZ3RoIjoxLCJQcm9wZXJ0eU5hbWUiOiJOYW1lIiwiUHJvcGVydHlWYWx1ZSI6IngifSwiYXR0ZW1wdGVkVmFsdWUiOiJ4Iiwic2V2ZXJpdHkiOjAsImVycm9yQ29kZSI6Ikxlbmd0aFZhbGlkYXRvciJ9XQ==

After decoding it will look like this:

[
    {
        "propertyName": "Name",
        "errorMessage": "'{PropertyName}' must be between {MinLength} and {MaxLength} characters. You entered {TotalLength} characters.",
        "placeholders": {
            "MinLength": 3,
            "MaxLength": 30,
            "TotalLength": 1,
            "PropertyName": "Name",
            "PropertyValue": "x"
        },
        "attemptedValue": "x",
        "severity": 0,
        "errorCode": "LengthValidator"
    }
]

Follow FluentValidation docs for more details.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  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. 
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
1.0.2 125 6/22/2022
1.0.1 104 5/28/2022