Skip to content

Commit

Permalink
Make ValidateParams take an interface instead of netHttpContext only
Browse files Browse the repository at this point in the history
  • Loading branch information
EwenQuim committed Dec 22, 2024
1 parent 0a1f2a2 commit b4be136
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
11 changes: 6 additions & 5 deletions ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,6 @@ func NewNetHTTPContext[B any](w http.ResponseWriter, r *http.Request, options re
return c
}

var (
_ ContextWithBody[any] = &netHttpContext[any]{} // Check that ContextWithBody implements Ctx.
_ ContextWithBody[string] = &netHttpContext[string]{} // Check that ContextWithBody implements Ctx.
)

// ContextWithBody is the same as fuego.ContextNoBody, but
// has a Body. The Body type parameter represents the expected data type
// from http.Request.Body. Please do not use a pointer as a type parameter.
Expand All @@ -142,6 +137,12 @@ type netHttpContext[Body any] struct {
readOptions readOptions
}

var (
_ ContextWithBody[any] = &netHttpContext[any]{} // Check that ContextWithBody implements Ctx.
_ ContextWithBody[string] = &netHttpContext[string]{} // Check that ContextWithBody implements Ctx.
_ ValidableCtx = &netHttpContext[any]{} // Check that ContextWithBody implements ValidableCtx.
)

// SetStatus sets the status code of the response.
// Alias to http.ResponseWriter.WriteHeader.
func (c netHttpContext[B]) SetStatus(code int) {
Expand Down
11 changes: 11 additions & 0 deletions internal/common_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ type CommonContext[B any] struct {

type ParamType string // Query, Header, Cookie

// GetOpenAPIParams returns the OpenAPI parameters declared in the OpenAPI spec.
func (c CommonContext[B]) GetOpenAPIParams() map[string]OpenAPIParam {
return c.OpenAPIParams
}

func (c CommonContext[B]) Context() context.Context {
return c.CommonCtx
}
Expand Down Expand Up @@ -71,6 +76,12 @@ func (c CommonContext[B]) QueryParams() url.Values {
return c.UrlValues
}

// HasQueryParam returns true if the query parameter with the given name exists.
func (c CommonContext[B]) HasQueryParam(name string) bool {
_, ok := c.UrlValues[name]
return ok
}

// QueryParam returns the query parameter with the given name.
// If it does not exist, it returns an empty string, unless there is a default value declared in the OpenAPI spec.
//
Expand Down
3 changes: 2 additions & 1 deletion serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ func HTTPHandler[ReturnType, Body any](s *Server, controller func(c ContextWithB
templates: templates,
}

err := validateParams(*ctx)
// PARAMS VALIDATION
err := ValidateParams(ctx)
if err != nil {
err = s.ErrorHandler(err)
s.SerializeError(w, r, err)
Expand Down
14 changes: 11 additions & 3 deletions validate_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@ package fuego

import "fmt"

func validateParams[B any](c netHttpContext[B]) error {
for k, param := range c.OpenAPIParams {
type ValidableCtx interface {
GetOpenAPIParams() map[string]OpenAPIParam
HasQueryParam(key string) bool
HasHeader(key string) bool
HasCookie(key string) bool
}

// ValidateParams checks if all required parameters are present in the request.
func ValidateParams(c ValidableCtx) error {
for k, param := range c.GetOpenAPIParams() {
if param.Default != nil {
// skip: param has a default
continue
Expand All @@ -12,7 +20,7 @@ func validateParams[B any](c netHttpContext[B]) error {
if param.Required {
switch param.Type {
case QueryParamType:
if !c.UrlValues.Has(k) {
if !c.HasQueryParam(k) {
err := fmt.Errorf("%s is a required query param", k)
return BadRequestError{
Title: "Query Param Not Found",
Expand Down

0 comments on commit b4be136

Please sign in to comment.