gv

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: May 6, 2025 License: MIT Imports: 8 Imported by: 0

README

gorilla-validator

PkgGoDev Go Report Card GitHub Actions Workflow Status Codacy coverage Codacy Badge

HTTP request validation middleware for Gorilla Mux.

It simplifies the process of validating and extracting data from various HTTP request sources, including URL parameters, query strings, form data, JSON, and XML.

Installation

go get github.com/iamolegga/gorilla-validator

Features

  • Easy to use middleware for Gorilla Mux
  • Supports multiple data sources: URL parameters, query strings, form data, JSON, and XML
  • Type-safe access to validated data
  • Leverages go-playground/validator for validation rules
  • Automatic HTTP 400 responses for invalid requests

Examples

Here are examples showing how to use gorilla-validator with different data sources:

URL Parameters
// URL parameters validation example
// Define the schema for URL parameters
type Params struct {
    ID int `schema:"id" validate:"required,gt=0"`
}

router.HandleFunc("/users/{id}", func(w http.ResponseWriter, r *http.Request) {
    // Get validated URL parameters
    params := gv.Validated[*Params](r, gv.Params)
}).Methods("GET").Use(gv.Validate(Params{}, gv.Params))
Query Parameters
// Query parameters validation example
// Define the schema for query parameters
type Query struct {
    Page  int    `schema:"page" validate:"omitempty,gte=1"`
    Limit int    `schema:"limit" validate:"omitempty,gte=1,lte=100"`
    Sort  string `schema:"sort" validate:"omitempty,oneof=name email date"`
}

router.HandleFunc("/users", func(w http.ResponseWriter, r *http.Request) {
    // Get validated query parameters
    query := gv.Validated[*Query](r, gv.Query)
}).Methods("GET").Use(gv.Validate(Query{}, gv.Query))
JSON Body
// JSON body validation example
// Define the schema for JSON body
// NOTE: You must set `json` tags for all fields you want to decode from JSON.
// The standard library does NOT use `schema` tags for JSON or XML decoding.
type BodyJSON struct {
    Name     string `json:"name" schema:"name" validate:"required,min=2"`
    Email    string `json:"email" schema:"email" validate:"required,email"`
    Password string `json:"password" schema:"password" validate:"required,min=8"`
}

router.HandleFunc("/users", func(w http.ResponseWriter, r *http.Request) {
    // Get validated JSON body
    data := gv.Validated[*BodyJSON](r, gv.JSON)
}).Methods("POST").Use(gv.Validate(BodyJSON{}, gv.JSON))
Form Data
// Form data validation example
// Define the schema for form data
// NOTE: For form/query/params, only the `schema` tag is needed.
type BodyForm struct {
    Email    string `schema:"email" validate:"required,email"`
    Password string `schema:"password" validate:"required"`
    Remember bool   `schema:"remember"`
}

router.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) {
    // Get validated form data
    loginData := gv.Validated[*BodyForm](r, gv.Form)
}).Methods("POST").Use(gv.Validate(BodyForm{}, gv.Form))
Multiple Validators
// Multiple validators example
// Define schemas for URL parameters and JSON body
// NOTE: Always specify `json` or `xml` tags for JSON/XML, and `schema` for form/query/params.
type MultiParams struct {
    ID int `schema:"id" validate:"required,gt=0"`
}

type MultiBody struct {
    Name  *string `json:"name" schema:"name" validate:"omitempty,min=2"`
    Email *string `json:"email" schema:"email" validate:"omitempty,email"`
}

router.HandleFunc("/users/{id}", func(w http.ResponseWriter, r *http.Request) {
    // Get validated URL parameters and JSON body
    params := gv.Validated[*MultiParams](r, gv.Params)
    updateData := gv.Validated[*MultiBody](r, gv.JSON)
}).Methods("PATCH").
    Use(gv.Validate(MultiParams{}, gv.Params)).
    Use(gv.Validate(MultiBody{}, gv.JSON))

Struct Tag Requirements

Important:

  • The Go standard library decoders (encoding/json, encoding/xml) do not use the schema tag.
  • You must specify json and/or xml tags for fields you want to decode from JSON or XML.
  • The schema tag is only used for form, query, and params sources.
  • If a tag is missing, the field name (case-sensitive) will be used by the decoder, which may not match your input.

Validation Rules

Validation rules are defined using struct tags with the go-playground/validator syntax. For a complete list of available validation rules, see: https://github.com/go-playground/validator

Sources

The library supports the following sources for validation:

  • gv.Params: URL parameters from Gorilla Mux
  • gv.Query: Query string parameters
  • gv.Form: Form data from POST requests
  • gv.JSON: JSON request body
  • gv.XML: XML request body

Error Handling

By default, the middleware will automatically respond with HTTP 400 (Bad Request) when validation fails. This behavior can be customized by using the gv.ErrorHandler function:

gv.ErrorHandler(func(err error) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		http.Error(w, "Invalid request: "+err.Error(), http.StatusBadRequest)
	}
})

Documentation

Overview

Package gv (gorilla-validator) provides HTTP request validation middleware for Gorilla Mux.

It simplifies the process of validating and extracting data from various HTTP request sources, including URL parameters, query strings, form data, JSON, and XML.

Features

  • Easy to use middleware for Gorilla Mux
  • Supports multiple data sources: URL parameters, query strings, form data, JSON, and XML
  • Type-safe access to validated data
  • Leverages go-playground/validator for validation rules
  • Automatic HTTP 400 responses for invalid requests

Example

Here's an example demonstrating how to use gorilla-validator with different data sources:

package main

import (
	"encoding/json"
	"net/http"

	"github.com/gorilla/mux"
	gv "github.com/iamolegga/gorilla-validator"
)

func main() {
	router := mux.NewRouter()

	// URL parameters validation example
	// Define the schema for URL parameters
	type Params struct {
		ID int `schema:"id" validate:"required,gt=0"`
	}

	router.HandleFunc("/users/{id}", func(w http.ResponseWriter, r *http.Request) {
		// Get validated URL parameters
		params := gv.Validated[*Params](r, gv.Params)
	}).Methods("GET").Use(gv.Validate(Params{}, gv.Params))

	// Query parameters validation example
	// Define the schema for query parameters
	type Query struct {
		Page  int    `schema:"page" validate:"omitempty,gte=1"`
		Limit int    `schema:"limit" validate:"omitempty,gte=1,lte=100"`
		Sort  string `schema:"sort" validate:"omitempty,oneof=name email date"`
	}

	router.HandleFunc("/users", func(w http.ResponseWriter, r *http.Request) {
		// Get validated query parameters
		query := gv.Validated[*Query](r, gv.Query)
	}).Methods("GET").Use(gv.Validate(Query{}, gv.Query))

	// JSON body validation example
	// Define the schema for JSON body
	type BodyJSON struct {
		Name     string `json:"name" schema:"name" validate:"required,min=2"`
		Email    string `json:"email" schema:"email" validate:"required,email"`
		Password string `json:"password" schema:"password" validate:"required,min=8"`
	}

	router.HandleFunc("/users", func(w http.ResponseWriter, r *http.Request) {
		// Get validated JSON body
		data := gv.Validated[*BodyJSON](r, gv.JSON)
	}).Methods("POST").Use(gv.Validate(BodyJSON{}, gv.JSON))

	// Form data validation example
	// Define the schema for form data
	type BodyForm struct {
		Email    string `schema:"email" validate:"required,email"`
		Password string `schema:"password" validate:"required"`
		Remember bool   `schema:"remember"`
	}

	router.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) {
		// Get validated form data
		loginData := gv.Validated[*BodyForm](r, gv.Form)
	}).Methods("POST").Use(gv.Validate(BodyForm{}, gv.Form))

	// Multiple validators example
	// Define schemas for URL parameters and JSON body
	type MultiParams struct {
		ID int `schema:"id" validate:"required,gt=0"`
	}

	type MultiBody struct {
		Name  *string `json:"name" schema:"name" validate:"omitempty,min=2"`
		Email *string `json:"email" schema:"email" validate:"omitempty,email"`
	}

	router.HandleFunc("/users/{id}", func(w http.ResponseWriter, r *http.Request) {
		// Get validated URL parameters and JSON body
		params := gv.Validated[*MultiParams](r, gv.Params)
		updateData := gv.Validated[*MultiBody](r, gv.JSON)
	}).Methods("PATCH").
		Use(gv.Validate(MultiParams{}, gv.Params)).
		Use(gv.Validate(MultiBody{}, gv.JSON))

	http.ListenAndServe(":8080", router)
}

Validation Rules

Validation rules are defined using struct tags with the go-playground/validator syntax. For a complete list of available validation rules, see: https://github.com/go-playground/validator

Sources

The library supports the following sources for validation:

  • gv.Params: URL parameters from Gorilla Mux
  • gv.Query: Query string parameters
  • gv.Form: Form data from POST requests
  • gv.JSON: JSON request body
  • gv.XML: XML request body

Error Handling

By default, the middleware will automatically respond with HTTP 400 (Bad Request) when validation fails. This behavior can be customized by using the `gv.ErrorHandler` function:

gv.ErrorHandler(func(err error) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		http.Error(w, "Invalid request: "+err.Error(), http.StatusBadRequest)
	}
})

Index

Constants

This section is empty.

Variables

View Source
var SchemaDecoder = schema.NewDecoder()

SchemaDecoder is an instance of the schema decoder from the gorilla/schema package, could be used for setting custom options

Functions

func ErrorHandler

func ErrorHandler(h ErrorHandlerFunc)

ErrorHandler allows setting a custom error handler function

func Validate

func Validate(schema any, src Source) mux.MiddlewareFunc

Validate is a middleware factory function that validates the input data based on the provided schema and source

func Validated

func Validated[T any](r *http.Request, src Source) T

Validated is a function that returns the validated data from the request context

func Validator added in v1.0.4

func Validator(v *validator.Validate)

Validator allows setting a custom validator instance

Types

type ErrorHandlerFunc

type ErrorHandlerFunc func(err error) http.HandlerFunc

ErrorHandlerFunc is a function type that defines how validation errors are handled

type Source

type Source string

Source represents the source of input data for validation

const (
	Params Source = "Params"
	Query  Source = "Query"
	Form   Source = "Form"
	JSON   Source = "JSON"
	XML    Source = "XML"
)

Jump to

Keyboard shortcuts

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