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) : settings_(settings) {}
23Middleware::~Middleware() =
default;
25void Middleware::PostRecvMessage(
26 ugrpc::client::MiddlewareCallContext& context,
27 const google::protobuf::Message& message
29 const ValidationSettings& settings = settings_.Get(context.GetCallName());
30 const ValidationResult result = ValidateMessage(message, {.fail_fast = settings.fail_fast});
31 if (result.IsSuccess()) {
34 const ValidationError& error = result.GetError();
35 switch (error.GetType()) {
36 case ValidationError::Type::kInternal:
37 throw ValidatorError(context.GetCallName());
38 case ValidationError::Type::kRule:
39 LOG_WARNING() << error;
40 throw ResponseError(context.GetCallName(), error.GetViolations());
42 UINVARIANT(
false,
"Unexpected error type");