- Notifications
You must be signed in to change notification settings - Fork0
GRPC request validator middleware with detailed error response for proper client side localization support.
License
CyberAndrii/CyberAndrii.Grpc.AspNetCore.Validation
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
GRPC request validator middleware with detailed error response for proper client side localization support.
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");
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
Uh oh!
There was an error while loading.Please reload this page.