Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add request validation for check requests #114

Merged
merged 1 commit into from
Jun 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions internal/service/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down
Loading