Skip to content

Commit

Permalink
Use validation library in pools creation endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
laouji committed Feb 10, 2025
1 parent 4d1e9d3 commit e6cc95c
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 28 deletions.
17 changes: 5 additions & 12 deletions internal/api/v2/handler_pools_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand All @@ -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()
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion internal/api/v2/handler_pools_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion internal/api/v2/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
17 changes: 5 additions & 12 deletions internal/api/v3/handler_pools_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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()
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion internal/api/v3/handler_pools_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion internal/api/v3/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit e6cc95c

Please sign in to comment.