validate

package
v0.0.26 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 24, 2026 License: MIT Imports: 1 Imported by: 0

README

validate middleware

Applies the core validation engine to non-typed handlers. Typed routes already perform tag and fh.Validator validation automatically.

type CreateUser struct {
    Name string `json:"name" validate:"required,min=2"`
}

app.Post("/users",
    validate.Body(func() any { return &CreateUser{} }),
    func(c fh.Ctx) error {
        return c.SendStatus(fh.StatusCreated)
    },
)

validate.Query(value) and validate.Headers(value) bind and validate query or header structs. Configure OnError for custom validation responses and Skip for explicit bypasses.

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)

Types

type Config

type Config struct {
	// Skip returns true to skip validation for this request.
	Skip func(fh.Ctx) bool

	// OnError is called when validation fails. If nil, returns 422 with problem details.
	OnError func(fh.Ctx, *fh.ValidationError) error
}

Config controls validation middleware behavior.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL