Go Validator
A lightweight, extensible, and framework-agnostic validation library for Go. Easily validate request bodies, nested objects, and more with a simple and intuitive API.
Features
- Framework-Agnostic: Works seamlessly with any Go application, not tied to specific frameworks.
- Customizable Validation Rules: Define and combine multiple validators per field with custom error messages.
- Support for Nested Structures: Validate complex data structures including maps, slices, and deeply nested structs.
- Middleware Integration: Includes a Gin adapter for easy integration into Gin-based web applications.
- Extensibility: Easily extend functionality by creating custom validators.
Why Use go-validator?
The go-validator
package offers several compelling reasons to choose it as your validation solution in Go applications:
- ✅ Lightweight & Efficient: Minimal overhead with robust validation capabilities.
- ✅ Framework-Agnostic: Works seamlessly across any Go application.
- ✅ Customizable Validation Rules: Define and combine multiple validators per field effortlessly.
- ✅ Support for Complex Data Structures: Easily handle nested objects, slices, maps, and structs.
- ✅ Extensible Architecture: Extend functionality through plugins or custom implementations.
- ✅ User-Friendly API: Intuitive interface for quick implementation.
- ✅ Comprehensive Error Messages: Clear error messages for faster debugging.
Installation
Install the latest version of go-validator
via:
For Gin framework integration, also install the Gin adapter:
Note: Ensure you are using Go 1.18+ for compatibility.
Basic Usage
Here’s how you can integrate go-validator
into a basic Gin application:
Step 1: Import the Package
Import the core validator package in your Go code:
If you're using Gin, import the Gin adapter as well:
Step 2 : Usage with Gin
Below is an example of how to use go-validator
with the Gin framework to validate incoming JSON payloads.
Step 1: Run the Application
Step 2: Verify the server is running
- Open a web browser or use
curl
to send a POST request tohttp://localhost:8080/user
with a JSON body.
Step 3: Test the Endpoint
Test with Valid Data:
Expected response:
Test with Invalid Data:
Expected response:
Contributing
We welcome contributions! If you find a bug or have an idea for a new feature, please open an issue or submit a pull request.
How to Contribute
- Fork the repository.
- Create a new branch (
git checkout -b feature/your-feature-name
). - Commit your changes (
git commit -m 'Add some feature'
). - Push to the branch (
git push origin feature/your-feature-name
). - Open a pull request.
License
This project is licensed under the MIT License. See the LICENSE file for details.