CyberAndrii.Grpc.AspNetCore.Validation
1.0.2
dotnet add package CyberAndrii.Grpc.AspNetCore.Validation --version 1.0.2
NuGet\Install-Package CyberAndrii.Grpc.AspNetCore.Validation -Version 1.0.2
<PackageReference Include="CyberAndrii.Grpc.AspNetCore.Validation" Version="1.0.2" />
paket add CyberAndrii.Grpc.AspNetCore.Validation --version 1.0.2
#r "nuget: CyberAndrii.Grpc.AspNetCore.Validation, 1.0.2"
// 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.
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 | Versions 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. |
-
net6.0
- FluentValidation (>= 11.0.1)
- Grpc.AspNetCore (>= 2.46.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.