Closed
Description
When trying to upgrade from 2.27.0 to 2.28.0, I noticed a change in validation of boolean query parameters. Taking the example from the README (replacing Chi with default router for simplicity) and adding a query parameter to the request demonstrates this change:
package main
import (
"context"
"fmt"
"net/http"
"github.com/danielgtaylor/huma/v2"
"github.com/danielgtaylor/huma/v2/adapters/humago"
"github.com/danielgtaylor/huma/v2/humacli"
)
type Options struct {
Port int `help:"Port to listen on" short:"p" default:"8888"`
}
type GreetingOutput struct {
Body struct {
Message string `json:"message" example:"Hello, world!" doc:"Greeting message"`
}
}
func main() {
cli := humacli.New(func(hooks humacli.Hooks, options *Options) {
api := humago.New(http.DefaultServeMux, huma.DefaultConfig("My API", "1.0.0"))
huma.Get(api, "/greeting/{name}", func(ctx context.Context, input *struct{
Boolean bool `query:"boolean"` // This is the query parameter added.
Name string `path:"name" maxLength:"30" example:"world" doc:"Name to greet"`
}) (*GreetingOutput, error) {
resp := &GreetingOutput{}
resp.Body.Message = fmt.Sprintf("Hello, %s!", input.Name)
return resp, nil
})
hooks.OnStart(func() {
http.ListenAndServe(fmt.Sprintf(":%d", options.Port),nil)
})
})
cli.Run()
}
With Huma v2.27.0, running curl 'localhost:8888/greeting/world?boolean=invalid' | jq
outputs:
{
"$schema": "http://localhost:8888/schemas/ErrorModel.json",
"title": "Unprocessable Entity",
"status": 422,
"detail": "validation failed",
"errors": [
{
"message": "invalid boolean",
"location": "query.boolean",
"value": "invalid"
}
]
}
However, Huma v2.28.0 replies to the same command with:
{
"$schema": "http://localhost:8888/schemas/ErrorModel.json",
"title": "Unprocessable Entity",
"status": 422,
"detail": "validation failed",
"errors": [
{
"message": "invalid boolean",
"location": "query.boolean",
"value": "invalid"
},
{
"message": "expected boolean",
"location": "query.boolean"
}
]
}
It doesn't seem to be an intentional change since it's not documented and it doesn't make much sense to returns the same error twice.
Metadata
Assignees
Labels
No labels
Activity