Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

License

NotificationsYou must be signed in to change notification settings

CyberAndrii/CyberAndrii.Grpc.AspNetCore.Validation

Repository files navigation

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

Build and PublishNuget

Setup

Create a new validator usingFluentValidation library:

usingFluentValidation;publicclassHelloRequestValidator:AbstractValidator<HelloRequest>{publicHelloRequestValidator(){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=()=>newKeepPlaceholdersMessageFormatter();

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"    }]

FollowFluentValidation docs for more details.

About

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

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp