Skip to main content

Overview

The validator middleware provides declarative request validation with built-in rules for common validation needs. It validates query parameters, form fields, headers, and path parameters. Use it when you need:
  • Input validation
  • Form validation
  • API parameter validation
  • Custom validation rules

Installation

Quick Start

Built-in Rules

Examples

Basic Validation

Optional Fields

Enum Validation

JSON Body Validation

Custom Error Handler

Custom Error Messages

Multiple Validation Groups

Query Parameter Validation

API Reference

Functions

Types

Field Sources

The validator checks these sources in order:
  1. Query parameters
  2. Form values (POST/PUT)
  3. Headers
  4. Path parameters

Error Response

Default error response format:

Technical Details

Architecture

The validator middleware is built on a pipeline architecture that processes validation rules sequentially:
  1. Field Value Extraction: The middleware retrieves field values from multiple sources in order of precedence:
    • Query parameters (highest priority)
    • Form values (POST/PUT requests)
    • Headers
    • Path parameters (lowest priority)
  2. Rule Application: Each validation rule is parsed and applied to the field value:
    • Rule format: ruleName or ruleName:parameter
    • Rules are checked sequentially for each field
    • First validation failure stops checking additional rules for that field
  3. Error Handling: Validation errors are collected and either:
    • Passed to a custom error handler (if provided)
    • Returned as JSON with 400 Bad Request (default)

Implementation Details

Validation Flow:
Rule Parsing:
  • Rules are split on : to separate the rule name from its parameter
  • The applyRule function handles all built-in validation logic
  • Unknown rules are silently ignored
Optional Fields:
  • When a field is marked as optional and empty, all its rules are skipped
  • Empty is defined as an empty string ("")
  • If an optional field has a value, all rules are applied normally
JSON Validation:
  • Reads the entire request body into memory
  • Parses JSON into map[string]any
  • Converts non-string values using stringify helper
  • Supports string, float64, and bool type conversion
Custom Messages:
  • The Message field in a Rule overrides default error messages
  • Custom messages apply to all validation rules for that field
  • If not set, each rule has its own default message

Performance Characteristics

  • Memory: Minimal allocation for most validations; JSON validation reads entire body
  • CPU: Linear with number of fields and rules; regex-free for better performance
  • Concurrency: Safe for concurrent use; no shared state between requests

Best Practices

  • Validate all user input
  • Use appropriate rules for data types
  • Provide clear error messages
  • Validate early in the middleware chain
  • Use optional fields for non-required parameters
  • Combine multiple rules for comprehensive validation
  • Consider custom error handlers for consistent API responses

Testing

Test Coverage

The validator middleware includes comprehensive test coverage for all validation rules and scenarios: