Documentation
¶
Overview ¶
Package validate provides request validation middleware for fh.
The validation engine (struct tags, rules, Validator interface) lives in the core fh package. This package provides middleware that wires it into the request lifecycle.
Usage with typed routes (automatic — no middleware needed):
type CreateUserRequest struct {
Name string `json:"name" validate:"required,min=2"`
Email string `json:"email" validate:"required,email"`
}
app.PostTyped("/users", func(c fh.Ctx, req CreateUserRequest) (UserResponse, error) {
// Validation already ran before this handler
return createUser(req), nil
})
Usage with middleware (for non-typed routes):
app.Post("/users", validate.Body(func() any { return &CreateUserRequest{} }), handler)
app.Get("/search", validate.Query(&SearchQuery{}), handler)
app.Get("/data", validate.Headers(&AuthHeaders{}), handler)
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Body ¶
func Body(factory func() any, cfg ...Config) fh.HandlerFunc
Body returns a middleware that decodes the request body into a new instance of the factory's return type, then validates it using struct tags and the Validator interface.
type CreateUserRequest struct {
Name string `json:"name" validate:"required,min=2"`
Email string `json:"email" validate:"required,email"`
}
app.Post("/users", validate.Body(func() any {
return &CreateUserRequest{}
}), handler)
func Headers ¶
func Headers(v any, cfg ...Config) fh.HandlerFunc
Headers returns a middleware that decodes request headers into v and validates.
type AuthHeaders struct {
Authorization string `header:"Authorization" validate:"required"`
}
app.Get("/data", validate.Headers(&AuthHeaders{}), handler)
func Query ¶
func Query(v any, cfg ...Config) fh.HandlerFunc
Query returns a middleware that decodes query parameters into v and validates.
type SearchQuery struct {
Q string `query:"q" validate:"required,min=1"`
Page int `query:"page" validate:"min=1"`
}
app.Get("/search", validate.Query(&SearchQuery{}), handler)