From 75fa24e0d547e283d706d4340ec94dc856cd293e Mon Sep 17 00:00:00 2001 From: Jonathan Marcantonio Date: Thu, 27 Jun 2024 15:55:24 -0400 Subject: [PATCH] Add request validation for check requests Signed-off-by: Jonathan Marcantonio --- internal/service/check.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/internal/service/check.go b/internal/service/check.go index f792833..915102a 100644 --- a/internal/service/check.go +++ b/internal/service/check.go @@ -6,6 +6,7 @@ import ( "github.com/project-kessel/relations-api/internal/biz" + "github.com/go-kratos/kratos/v2/errors" "github.com/go-kratos/kratos/v2/log" pb "github.com/project-kessel/relations-api/api/relations/v0" @@ -25,6 +26,21 @@ func NewCheckService(logger log.Logger, checkUseCase *biz.CheckUsecase) *CheckSe } func (s *CheckService) Check(ctx context.Context, req *pb.CheckRequest) (*pb.CheckResponse, error) { + if err := req.ValidateAll(); err != nil { + s.log.Infof("Request failed to pass validation: %v", req) + return nil, errors.BadRequest("Invalid request", err.Error()) + } + + if err := req.Subject.ValidateAll(); err != nil { + s.log.Infof("Subject failed to pass validation: %v", req) + return nil, errors.BadRequest("Invalid request", err.Error()) + } + + if err := req.Resource.ValidateAll(); err != nil { + s.log.Infof("Resource failed to pass validation: %v", req) + return nil, errors.BadRequest("Invalid request", err.Error()) + } + resp, err := s.check.Check(ctx, req) if err != nil { return resp, fmt.Errorf("failed to perform check: %w", err)