An open API service indexing awesome lists of open source software.

https://github.com/vihuvac/go-openapi-validator

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.
https://github.com/vihuvac/go-openapi-validator

golang openapi swagger swagger-ui

Last synced: 3 days ago
JSON representation

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.

Awesome Lists containing this project

README

          


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](https://github.com/cdimascio/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](https://github.com/gorilla/mux), and [Gin](https://github.com/gin-gonic/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

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

### 2. Usage Examples

#### Standard Library (`net/http`)

```go
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

```text
.
โ”œโ”€โ”€ 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:

```bash
# 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](./CONTRIBUTING.md) to get started.

## ๐Ÿ“„ License

This project is licensed under the **MIT License**. See the [LICENSE](./LICENSE) file for more details.

## Additional Resources

### Project
- [Changelog](./CHANGELOG.md)
- [Security Policy](./SECURITY.md)
- [Code of Conduct](./CODE_OF_CONDUCT.md)

### Tools
- [Swagger UI](https://swagger.io/tools/swagger-ui/)
- [OpenAPI Specification v3](https://swagger.io/specification/v3/)
- [Kin OpenAPI](https://github.com/getkin/kin-openapi)