querylimits

package
v4.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package querylimits provides stable core query-parameter guardrail middleware.

Use New with Options when handlers need bounded query parameter counts, key lengths, value lengths, and pagination limit values before application parsing. Middleware and Handler expose the same guardrail for ports.Middleware wiring or direct net/http usage.

Invalid query shapes fail with Problem Details 400 responses and do not reach the wrapped handler. The package does not parse business filters or database queries; use queryparams for typed collection-query parsing after these size and limit checks pass.

Purpose: See the package summary above. Import: `github.com/aatuh/api-toolkit/v4/middleware/querylimits`. Example: See docs/api-reference.md for package example links and docs/cookbook.md for task recipes. Errors: Constructors, parsers, and handlers return or write documented errors according to their signatures; packages with plain data types do not add hidden error channels. Concurrency: Treat configured middleware and helpers as immutable after construction; request and response values remain request-scoped unless a type documents stronger guarantees. Stability: Stable core API under VERSIONING.md and scripts/apicheck.sh. When not to use: Prefer net/http, application-owned types, or narrower helpers when this package contract is not needed.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ErrorWriter

type ErrorWriter func(http.ResponseWriter, int, httpx.Problem)

ErrorWriter allows overriding how validation errors are written.

type Middleware

type Middleware struct {
	// contains filtered or unexported fields
}

Middleware enforces query parameter limits.

func New

func New(opts Options) (*Middleware, error)

New constructs a query limits middleware with safe defaults.

Example
package main

import (
	"context"
	"fmt"
	"net/http"
	"net/http/httptest"

	"github.com/aatuh/api-toolkit/v4/middleware/querylimits"
)

func main() {
	middleware, err := querylimits.New(querylimits.Options{MaxLimit: 10})
	if err != nil {
		panic(err)
	}
	handler := middleware.Handler(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
		w.WriteHeader(http.StatusNoContent)
	}))

	recorder := httptest.NewRecorder()
	req := httptest.NewRequestWithContext(context.Background(), http.MethodGet, "/widgets?limit=5", nil)
	handler.ServeHTTP(recorder, req)

	fmt.Println(recorder.Code)

}
Output:
204

func (*Middleware) Handler

func (m *Middleware) Handler(next http.Handler) http.Handler

Handler wraps the next handler with query guardrails.

func (*Middleware) Middleware

func (m *Middleware) Middleware() func(http.Handler) http.Handler

Middleware implements ports.Middleware via Handler adapter.

type Options

type Options struct {
	MaxParams      int
	MaxKeyLength   int
	MaxValueLength int
	LimitParam     string
	MaxLimit       int
	ErrorWriter    ErrorWriter
}

Options configures query parameter limits.

Jump to

Keyboard shortcuts

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