Skip to content

vihuvac/go-openapi-validator

Go OpenAPI Validator Logo

Go OpenAPI Validator

A framework-agnostic OpenAPI v3 validator for Go, designed for performance and reliability.

Go Version License OpenAPI Version
Tests Status Coverage
Security Policy Code of Conduct PRs Welcome


πŸ“– Overview

Go OpenAPI Validator is a high-performance, framework-agnostic library for validating HTTP requests and responses against OpenAPI v3 specifications. Inspired by express-openapi-validator, it provides a robust middleware layer that ensures your API remains consistent with its documentation.

We focus on minimalism and reliability, with zero external testing dependencies and a lean footprint.

✨ Key Features

  • πŸš€ Framework Agnostic: Native support for net/http, Gorilla Mux, and Gin.
  • πŸ›‘οΈ Request Validation: Automatic validation of request bodies, query parameters, and headers.
  • βœ… Response Validation: Optional outgoing response validation to catch implementation errors.
  • πŸ“„ Swagger UI: Built-in, zero-config Swagger UI integration served at /docs.
  • πŸ§ͺ Professional Grade: Comprehensive test suite using only the Go standard library.
  • βš™οΈ Highly Configurable: Custom error encoders, router selection, and more.

πŸ› οΈ System Requirements

Before you begin, ensure you have the following installed:

  • Go: v1.21.x or higher (Tested with v1.23+)
  • Git: For version control

πŸš€ Getting Started

1. Installation

go get github.com/vihuvac/go-openapi-validator

2. Usage Examples

Standard Library (net/http)

package main

import (
	"log"
	"net/http"
	"github.com/getkin/kin-openapi/routers/legacy"
	validator "github.com/vihuvac/go-openapi-validator"
)

func main() {
	v, err := validator.New("openapi.yaml")
	if err != nil {
		log.Fatal(err)
	}

	// For net/http, use the legacy router
	r, _ := legacy.NewRouter(v.Swagger)
	validator.WithRouter(r)(v.Options)

	mux := http.NewServeMux()
	v.HandleSwaggerUI(mux)

	mux.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "application/json")
		w.Write([]byte(`{"message": "Hello World"}`))
	})

	log.Fatal(http.ListenAndServe(":8080", v.Middleware(mux)))
}

πŸ“‚ Project Structure

.
β”œβ”€β”€ docs/             # Documentation and assets
β”œβ”€β”€ examples/         # Router-specific implementation examples
β”‚   β”œβ”€β”€ gin/          # Gin-gonic integration
β”‚   β”œβ”€β”€ gorilla/      # Gorilla Mux integration
β”‚   └── standard/     # Standard net/http integration
β”œβ”€β”€ swagger-ui/       # Embedded Swagger UI assets
β”œβ”€β”€ errors.go         # Custom error handling and encoders
β”œβ”€β”€ options.go        # Configuration options (Functional options pattern)
β”œβ”€β”€ swagger.go        # Swagger UI serving logic
└── validator.go      # Core validation middleware

βš™οΈ Configuration

Option Description Default
WithValidateRequests(bool) Enable/Disable request validation true
WithValidateResponses(bool) Enable/Disable response validation false
WithSwaggerUIPath(string) Change Swagger UI base path /docs
WithErrorEncoder(ErrorEncoder) Custom error response format DefaultErrorEncoder
WithRouter(routers.Router) Set a custom OpenAPI router gorillamux.NewRouter

πŸ§ͺ Running Tests

Maintain code quality by running the comprehensive test suite:

# Run all unit tests
go test ./...

# Run tests with coverage
go test -coverprofile=coverage.out ./...
go tool cover -html=coverage.out

🀝 Contributing

Contributions are welcome! Check out the Contribution Guide to get started.

πŸ“„ License

This project is licensed under the MIT License. See the LICENSE file for more details.

Additional Resources

Project

Tools

About

Framework-agnostic OpenAPI v3 validator for Go. High-performance validation of requests (body, params, headers) and responses. Supports net/http, Gin, and Gorilla Mux. Includes zero-config Swagger UI, custom errors, and zero external test deps. Lean, reliable, production-ready.

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Packages

 
 
 

Contributors