https://github.com/lyonnee/hvalid
hvalid is a lightweight validation library written in Go language.(一个用Go语言编写的轻量级验证库)
https://github.com/lyonnee/hvalid
golang validation
Last synced: about 2 months ago
JSON representation
hvalid is a lightweight validation library written in Go language.(一个用Go语言编写的轻量级验证库)
- Host: GitHub
- URL: https://github.com/lyonnee/hvalid
- Owner: lyonnee
- License: mit
- Created: 2024-04-16T05:52:07.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2025-06-05T06:54:11.000Z (9 months ago)
- Last Synced: 2025-11-12T21:02:47.954Z (4 months ago)
- Topics: golang, validation
- Language: Go
- Homepage:
- Size: 65.4 KB
- Stars: 14
- Watchers: 2
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go - hvalid
- fucking-awesome-go - hvalid
- awesome-go-cn - hvalid
- awesome-go-plus - hvalid - 11-blue) (Validation / Utility/Miscellaneous)
- awesome-go-with-stars - hvalid - 06-05 | (Validation / Utility/Miscellaneous)
- awesome-go - hvalid
- awesome-go - hvalid
README
# hvalid
| English | [中文](README_zh.md) |
| --- | --- |
`hvalid` is a lightweight validation library written in Go language. It provides a custom validator interface and a series of common validation functions to help developers quickly implement data validation.
[](https://goreportcard.com/report/github.com/lyonnee/hvalid)

[](https://codecov.io/gh/lyonnee/hvalid)
## Features
- Generic support: Can validate any type of data, including basic types, structs, slices, etc.
- Easy to use: Offers a concise API for developers to quickly perform parameter validation.
- Extensible: Allows custom validation rules to meet different validation needs.
- Friendly error messages: Returns clear error messages when validation fails, making it easy for developers to locate issues.
## Installation
Install using the `go get` command:
```bash
go get github.com/lyonnee/hvalid
```
### Usage Examples
#### Basic Type Validation
```go
import (
"errors"
"github.com/lyonnee/hvalid"
)
func main() {
// Validate string length
err := hvalid.Validate[string]("hello", hvalid.MinLen[string](3))
if err != nil {
// Handle error
}
// Validate number range
err = hvalid.Validate[int](10, hvalid.Min(5), hvalid.Max(15))
if err != nil {
// Handle error
}
}
```
#### Struct Validation
```go
type User struct {
Name string
Email string
Age int
}
func UserValidator() hvalid.ValidatorFunc[User] {
return hvalid.ValidatorFunc[User](func(user User) error {
if user.Age < 18 {
return errors.New("Age must be greater than 18")
}
return hvalid.Validate[string](user.Email, hvalid.Email())
})
}
func main() {
user := User{
Name: "Zhang San",
Email: "zhangsan@example.com",
Age: 20,
}
err := hvalid.Validate[User](user, UserValidator())
if err != nil {
// Handle error
}
}
```
#### Custom Validation Rules
```go
func IsPositive(errMsg ...string) hvalid.ValidatorFunc[int] {
return hvalid.ValidatorFunc[int](func(num int) error {
if num <= 0 {
if len(errMsg) > 0 {
return errors.New(errMsg[0])
}
return errors.New("The number must be positive")
}
return nil
})
}
func main() {
err := hvalid.Validate[int](10, IsPositive())
if err != nil {
// Handle error
}
}
```
## Testing
The project includes unit tests, run all tests with the `go test` command:
```bash
go test ./...
```
## Contributing
Issues and pull requests are welcome to improve `hvalid`.
## License
`hvalid` is released under the MIT License. See the [LICENSE](LICENSE) file for more information.