From e6cc95c2b666991fc788e0ef59f903286cdf0df2 Mon Sep 17 00:00:00 2001 From: Crimson Thompson Date: Mon, 10 Feb 2025 11:01:20 +0100 Subject: [PATCH] Use validation library in pools creation endpoint --- internal/api/v2/handler_pools_create.go | 17 +++++------------ internal/api/v2/handler_pools_create_test.go | 3 ++- internal/api/v2/router.go | 2 +- internal/api/v3/handler_pools_create.go | 17 +++++------------ internal/api/v3/handler_pools_create_test.go | 3 ++- internal/api/v3/router.go | 2 +- 6 files changed, 16 insertions(+), 28 deletions(-) diff --git a/internal/api/v2/handler_pools_create.go b/internal/api/v2/handler_pools_create.go index d291ba1a..143909bb 100644 --- a/internal/api/v2/handler_pools_create.go +++ b/internal/api/v2/handler_pools_create.go @@ -2,13 +2,13 @@ package v2 import ( "encoding/json" - "errors" "fmt" "net/http" "time" "github.com/formancehq/go-libs/v2/api" "github.com/formancehq/payments/internal/api/backend" + "github.com/formancehq/payments/internal/api/validation" "github.com/formancehq/payments/internal/models" "github.com/formancehq/payments/internal/otel" "github.com/google/uuid" @@ -17,15 +17,8 @@ import ( ) type CreatePoolRequest struct { - Name string `json:"name"` - AccountIDs []string `json:"accountIDs"` -} - -func (r *CreatePoolRequest) Validate() error { - if len(r.AccountIDs) == 0 { - return errors.New("one or more account id required") - } - return nil + Name string `json:"name" validate:""` + AccountIDs []string `json:"accountIDs" validate:"min=1,dive,accountID"` } type PoolResponse struct { @@ -34,7 +27,7 @@ type PoolResponse struct { Accounts []string `json:"accounts"` } -func poolsCreate(backend backend.Backend) http.HandlerFunc { +func poolsCreate(backend backend.Backend, validator *validation.Validator) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { ctx, span := otel.Tracer().Start(r.Context(), "v2_poolsBalancesAt") defer span.End() @@ -49,7 +42,7 @@ func poolsCreate(backend backend.Backend) http.HandlerFunc { populateSpanFromCreatePoolRequest(span, CreatePoolRequest) - if err := CreatePoolRequest.Validate(); err != nil { + if _, err := validator.Validate(CreatePoolRequest); err != nil { otel.RecordError(span, err) api.BadRequest(w, ErrValidation, err) return diff --git a/internal/api/v2/handler_pools_create_test.go b/internal/api/v2/handler_pools_create_test.go index bed4d452..8f99d07a 100644 --- a/internal/api/v2/handler_pools_create_test.go +++ b/internal/api/v2/handler_pools_create_test.go @@ -6,6 +6,7 @@ import ( "net/http/httptest" "github.com/formancehq/payments/internal/api/backend" + "github.com/formancehq/payments/internal/api/validation" "github.com/formancehq/payments/internal/models" "github.com/google/uuid" . "github.com/onsi/ginkgo/v2" @@ -34,7 +35,7 @@ var _ = Describe("API v2 Pools Create", func() { w = httptest.NewRecorder() ctrl := gomock.NewController(GinkgoT()) m = backend.NewMockBackend(ctrl) - handlerFn = poolsCreate(m) + handlerFn = poolsCreate(m, validation.NewValidator()) }) It("should return a bad request error when body is missing", func(ctx SpecContext) { diff --git a/internal/api/v2/router.go b/internal/api/v2/router.go index 7243bc34..1d91878c 100644 --- a/internal/api/v2/router.go +++ b/internal/api/v2/router.go @@ -65,7 +65,7 @@ func newRouter(backend backend.Backend, info api.ServiceInfo, a auth.Authenticat // Pools r.Route("/pools", func(r chi.Router) { - r.Post("/", poolsCreate(backend)) + r.Post("/", poolsCreate(backend, validator)) r.Get("/", poolsList(backend)) r.Route("/{poolID}", func(r chi.Router) { diff --git a/internal/api/v3/handler_pools_create.go b/internal/api/v3/handler_pools_create.go index be01d69a..b33bbad0 100644 --- a/internal/api/v3/handler_pools_create.go +++ b/internal/api/v3/handler_pools_create.go @@ -2,13 +2,13 @@ package v3 import ( "encoding/json" - "errors" "fmt" "net/http" "time" "github.com/formancehq/go-libs/v2/api" "github.com/formancehq/payments/internal/api/backend" + "github.com/formancehq/payments/internal/api/validation" "github.com/formancehq/payments/internal/models" "github.com/formancehq/payments/internal/otel" "github.com/google/uuid" @@ -17,18 +17,11 @@ import ( ) type CreatePoolRequest struct { - Name string `json:"name"` - AccountIDs []string `json:"accountIDs"` + Name string `json:"name" validate:""` + AccountIDs []string `json:"accountIDs" validate:"min=1,dive,accountID"` } -func (r *CreatePoolRequest) Validate() error { - if len(r.AccountIDs) == 0 { - return errors.New("one or more account id required") - } - return nil -} - -func poolsCreate(backend backend.Backend) http.HandlerFunc { +func poolsCreate(backend backend.Backend, validator *validation.Validator) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { ctx, span := otel.Tracer().Start(r.Context(), "v3_poolsCreate") defer span.End() @@ -43,7 +36,7 @@ func poolsCreate(backend backend.Backend) http.HandlerFunc { populateSpanFromCreatePoolRequest(span, CreatePoolRequest) - if err := CreatePoolRequest.Validate(); err != nil { + if _, err := validator.Validate(CreatePoolRequest); err != nil { otel.RecordError(span, err) api.BadRequest(w, ErrValidation, err) return diff --git a/internal/api/v3/handler_pools_create_test.go b/internal/api/v3/handler_pools_create_test.go index 24152643..50853ea1 100644 --- a/internal/api/v3/handler_pools_create_test.go +++ b/internal/api/v3/handler_pools_create_test.go @@ -6,6 +6,7 @@ import ( "net/http/httptest" "github.com/formancehq/payments/internal/api/backend" + "github.com/formancehq/payments/internal/api/validation" "github.com/formancehq/payments/internal/models" "github.com/google/uuid" . "github.com/onsi/ginkgo/v2" @@ -34,7 +35,7 @@ var _ = Describe("API v3 Pools Create", func() { w = httptest.NewRecorder() ctrl := gomock.NewController(GinkgoT()) m = backend.NewMockBackend(ctrl) - handlerFn = poolsCreate(m) + handlerFn = poolsCreate(m, validation.NewValidator()) }) It("should return a bad request error when body is missing", func(ctx SpecContext) { diff --git a/internal/api/v3/router.go b/internal/api/v3/router.go index 6800829e..a196aca4 100644 --- a/internal/api/v3/router.go +++ b/internal/api/v3/router.go @@ -65,7 +65,7 @@ func newRouter(backend backend.Backend, info api.ServiceInfo, a auth.Authenticat // Pools r.Route("/pools", func(r chi.Router) { - r.Post("/", poolsCreate(backend)) + r.Post("/", poolsCreate(backend, validator)) r.Get("/", poolsList(backend)) r.Route("/{poolID}", func(r chi.Router) {