A powerful, lightweight, and dependency-free validation package for Go.
Explore the docs »
pkg.go.dev
·
Report Bug
·
Request Feature
- About The Project
- Features
- Built With
- Getting Started
- Execution Order
- Usage
- Available Validators
- Contributing
- License
- Author
Go Validators is a validation package for Go designed to be powerful, lightweight, and dependency-free. It provides a flexible way to handle data validation, allowing developers to declaratively validate structs from JSON payloads using validates tags or use individual validation functions for granular control.
This package is easily integrated into any Go project without introducing external dependencies, offering a simple yet extensible solution for ensuring data integrity.
- Struct Validation: Declaratively validate structs from JSON payloads using
validatestags. - Standalone Functions: Use individual validation functions for granular control over any value.
- Zero Dependencies: A lightweight package that can be integrated into any project without external dependencies.
- Customizable: Extend the library with your own logic using the
Customvalidator.
This project was developed using the following core technologies:
- Go – An open-source programming language designed for simplicity, reliability, and efficiency in building scalable software.
To get a local copy up and running, follow these steps.
go get github.com/dariomatias-dev/go-validatorsValidators should be organized in a three-stage order within validates tags:
- Presence Validators (
required): Confirm the field exists. - Type Validators (
isString,isInt, etc.): Ensure the value has the correct data type. - Value Validators (
minLength,email, etc.): Apply specific rules to the value's content.
This order prevents runtime errors by ensuring that value validators never receive an unexpected type.
Import the package using an alias for cleaner code:
import v "github.com/dariomatias-dev/go-validators"Validate a JSON payload by mapping it to a struct with validates tags. The Validate function returns a map of errors or nil on success.
type UserModel struct {
Name string `json:"name" validates:"required;isString;minLength=3;maxLength=20"`
Age int `json:"age" validates:"required;isInt;min=18;max=100"`
Email string `json:"email" validates:"required;email"`
}
user := UserModel{}
jsonPayload := `{
"name": "Name",
"age": 16,
"email": "emailexample@gmail.com"
}`
err := v.Validate(&user, jsonPayload)
if err != nil {
fmt.Println(err)
return
}Validators are configurable higher-order functions. The first parentheses configure the rule, the second receives the value.
value := 4
err, _ := v.Min(3, "Value must be at least 3.")(value)
if err != nil {
fmt.Println(err)
}
value = 2
err, _ = v.Min(3)(value)
if err != nil {
fmt.Println(err)
return
}All parameters marked with (*) are mandatory. Custom error messages are always optional.
| Validator | Category | Parameters | Applicable Value Type |
|---|---|---|---|
| Required | Presence | any | |
| IsString | Type | string | |
| IsNumber | Type | int, float | |
| IsInt | Type | int | |
| IsFloat | Type | float | |
| IsBool | Type | bool | |
| IsArray | Type | field validators string* | slice, array |
| IsNullString | Type | nil, string | |
| IsNullNumber | Type | nil, int, float | |
| IsNullInt | Type | nil, int | |
| IsNullFloat | Type | nil, float | |
| IsNullBool | Type | nil, bool | |
| IsNullArray | Type | field validators string* | nil, slice, array |
| Value | string | ||
| Password | Value | string | |
| Min | Value | minimum value (number)* | int, int32, int64, float32, float64 |
| Max | Value | maximum value (number)* | int, int32, int64, float32, float64 |
| Length | Value | size int* | string, slice, array |
| MinLength | Value | minimum size int* | string, slice, array |
| MaxLength | Value | maximum size int* | string, slice, array |
| IsAlpha | Value | string | |
| IsAlphaNum | Value | string | |
| IsAlphaSpace | Value | string | |
| IsAlphaNumSpace | Value | string | |
| StartsWith | Value | prefix string* | string |
| StartsNotWith | Value | prefix string* | string |
| EndsWith | Value | suffix string* | string |
| EndsNotWith | Value | suffix string* | string |
| Regex | Value | regex string* | string |
| Url | Value | string | |
| Uuid | Value | version int (Default: 5) | string |
| OneOf | Value | options []string* | string |
| Custom | Value | custom validator function* | any |
Contributions make the open-source community an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
- Fork the Project
- Create your Feature Branch
git checkout -b feature/AmazingFeature- Commit your Changes
git commit -m 'Add some AmazingFeature'- Push to the Branch
git push origin feature/AmazingFeature- Open a Pull Request
Distributed under the MIT License. See the LICENSE file for more information.
Developed by Dário Matias:
- Portfolio: dariomatias-dev
- GitHub: dariomatias-dev
- Email: matiasdario75@gmail.com
- Instagram: @dariomatias_dev
- LinkedIn: linkedin.com/in/dariomatias-dev