1#include <grpc-protovalidate/client/middleware.hpp>
5#include <google/protobuf/arena.h>
7#include <userver/grpc-protovalidate/client/exceptions.hpp>
8#include <userver/grpc-protovalidate/validate.hpp>
9#include <userver/logging/log.hpp>
10#include <userver/utils/assert.hpp>
12USERVER_NAMESPACE_BEGIN
14namespace grpc_protovalidate::client {
16const ValidationSettings& Settings::Get(std::string_view method_name)
const {
17 auto it = per_method.find(method_name);
18 return it != per_method.end() ? it->second : global;
21Middleware::Middleware(
const Settings& settings)
25Middleware::~Middleware() =
default;
27void Middleware::PostRecvMessage(
28 ugrpc::client::MiddlewareCallContext& context,
29 const google::protobuf::Message& message
31 const ValidationSettings& settings = settings_.Get(context.GetCallName());
32 const ValidationResult result = ValidateMessage(message, {.fail_fast = settings.fail_fast});
33 if (result.IsSuccess()) {
36 const ValidationError& error = result.GetError();
37 switch (error.GetType()) {
38 case ValidationError::Type::kInternal:
39 throw ValidatorError(context.GetCallName());
40 case ValidationError::Type::kRule:
41 LOG_WARNING() << error;
42 throw ResponseError(context.GetCallName(), error.GetViolations());
44 UINVARIANT(
false,
"Unexpected error type");