Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/go-the-way/validator
A lightweight model validator written in Go.
https://github.com/go-the-way/validator
Last synced: about 1 month ago
JSON representation
A lightweight model validator written in Go.
- Host: GitHub
- URL: https://github.com/go-the-way/validator
- Owner: go-the-way
- License: apache-2.0
- Created: 2022-03-08T02:03:57.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2022-05-11T07:33:08.000Z (over 2 years ago)
- Last Synced: 2024-04-22T13:32:30.276Z (8 months ago)
- Language: Go
- Size: 123 KB
- Stars: 7
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go-extra - validator - 03-08T02:03:57Z|2022-05-11T07:33:08Z| (Validation / Fail injection)
README
# validator
A lightweight model validator written in Go.
[![CircleCI](https://circleci.com/gh/go-the-way/validator/tree/main.svg?style=shield)](https://circleci.com/gh/go-the-way/validator/tree/main)
![GitHub go.mod Go version](https://img.shields.io/github/go-mod/go-version/go-the-way/validator)
[![codecov](https://codecov.io/gh/go-the-way/validator/branch/main/graph/badge.svg?token=8MAR3J959H)](https://codecov.io/gh/go-the-way/validator)
[![Go Report Card](https://goreportcard.com/badge/github.com/go-the-way/validator)](https://goreportcard.com/report/github.com/go-the-way/validator)
[![GoDoc](https://pkg.go.dev/badge/github.com/go-the-way/validator?status.svg)](https://pkg.go.dev/github.com/go-the-way/validator?tab=doc)
[![Release](https://img.shields.io/github/release/go-the-way/validator.svg?style=flat-square)](https://github.com/go-the-way/validator/releases)
[![Mentioned in Awesome Go](https://awesome.re/mentioned-badge.svg)](https://github.com/avelino/awesome-go#validation)## Features
- Supports multiple language.## quickstart
```go
package mainimport (
"fmt"
v "github.com/go-the-way/validator"
)func main() {
result := v.New(&struct {
int `validate:"min(10,fail)"`
}{}).Validate()
fmt.Println(result.Passed)
fmt.Println(result.Messages())
}
```## Custom validation implementation
```go
package mainimport (
"fmt"
v "github.com/go-the-way/validator"
"reflect"
)func main() {
v.Custom("mycustom", func(value reflect.Value) (bool, string) { return false, "mycustom validation." })
vv := v.New(&struct {
Name string `validate:"custom(mycustom)"`
}{}).Validate()
fmt.Println(vv.Passed)
fmt.Println(vv.Messages())
}
```## Validators
| Name | Support | Example | Description |
|--------------|---------------------------------------------------------------------------------|-------------------------------------|----------------------------------------------------------------------------------------------------------------------------------|
| Min | `([])(*)uint{8,64}`, `([])(*)int{8,64}`, `([])(*)float{32,64}` | validate:"min(N,invalid)" | `Every value` must be `>= N` |
| Max | `([])(*)uint{8,64}`, `([])(*)int{8,64}`, `([])(*)float{32,64}` | validate:"max(N,invalid)" | `Every value` must be `<= N` |
| Length | `(*)string`, `(*)Array[(*)string]`, `(*)Slice[(*)string]` | validate:"length(N,invalid)" | `(*)string`: `Value's Len` must be `== N`
`(*)Array[(*)string]` or `(*)Slice[(*)string]`: `Every Value's Len` must be `== N` |
| ArrLength | `(*)Array[(*)Any]`, `(*)Slice[(*)Any]` | validate:"arr_length(N,invalid)" | `(*)Array[(*)Any]` or `(*)Slice[(*)Any]`: `Array` or `Slice's Len` must be `== N` |
| MinLength | `(*)string`, `(*)Array[(*)string]`, `(*)Slice[(*)string]` | validate:"minlength(N,invalid)" | `(*)string`: `Value's Len` must be `>= N`
`(*)Array[(*)string]` or `(*)Slice[(*)string]`: `Every Value's Len` must be `>= N` |
| ArrMinLength | `(*)Array[(*)Any]`, `(*)Slice[(*)Any]` | validate:"arr_minlength(N,invalid)" | `(*)Array[(*)Any]` or `(*)Slice[(*)Any]`: `Array` or `Slice's Len` must be `>= N` |
| MaxLength | `(*)string`, `(*)Array[(*)string]`, `(*)Slice[(*)string]` | validate:"maxlength(N,invalid)" | `(*)string`: `Value's Len` must be `<= N`
`(*)Array[(*)string]` or `(*)Slice[(*)string]`: `Every Value's Len` must be `<= N` |
| ArrMaxLength | `(*)Array[(*)Any]`, `(*)Slice[(*)Any]` | validate:"arr_maxlength(N,invalid)" | `(*)Array[(*)Any]` or `(*)Slice[(*)Any]`: `Array` or `Slice's Len` must be `<= N` |
| Enum | `([])(*)uint{8,64}`, `([])(*)int{8,64}`, `([])(*)float{32,64}`, `([])(*)string` | validate:"enum(O,invalid)" | `Every value` must be one of `O` |
| Regex | `([])(*)string` | validate:"regex(RE,invalid)" | `Every value` must be match `RE` |
| Valid | `*struct{}` | validate:"valid(T,invalid)" | `Value` must be not `nil` |
| Custom | `any` | validate:"custom(CUSTOM)" | `CUSTOM` validation |