https://github.com/xgfone/go-validation
Provide a validation framework based on the built rule.
https://github.com/xgfone/go-validation
go golang rule ruler validate validation validation-library validation-rule validation-rules validator
Last synced: 6 months ago
JSON representation
Provide a validation framework based on the built rule.
- Host: GitHub
- URL: https://github.com/xgfone/go-validation
- Owner: xgfone
- License: apache-2.0
- Created: 2023-04-01T07:54:32.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2023-12-18T06:11:33.000Z (almost 2 years ago)
- Last Synced: 2025-02-11T09:37:11.978Z (8 months ago)
- Topics: go, golang, rule, ruler, validate, validation, validation-library, validation-rule, validation-rules, validator
- Language: Go
- Homepage:
- Size: 54.7 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Go Validation [](https://github.com/xgfone/go-validation/actions/workflows/go.yml) [](https://pkg.go.dev/github.com/xgfone/go-validation) [](https://raw.githubusercontent.com/xgfone/go-validation/master/LICENSE)
Provide a validation framework based on the built rule, supporting `Go1.16+`.
## Install
```shell
$ go get -u github.com/xgfone/go-validation
```## Example
For registering the validator and validating whether a value is valid, See [Builder](https://pkg.go.dev/github.com/xgfone/go-validation/#example-Builder).```go
package mainimport "github.com/xgfone/go-validation"
func main() {
// Validate whether an integer is in [min, max].
validation.Validate(123, `min(1) && max(200)`) // =>
validation.Validate(456, `min(1) && max(200)`) // => an error
validation.Validate(123, `ranger(1, 200)`) // =>
validation.Validate(456, `ranger(1, 200)`) // => an error// Validate whether an string is one of a string list.
validation.Validate("a", `oneof("a", "b", "c")`) // =>
validation.Validate("d", `oneof("a", "b", "c")`) // => an error// Validate whether an string is an integer string that can be parsed to an integer.
validation.Validate("123", `isinteger`) // =>
validation.Validate("+123", `isinteger`) // =>
validation.Validate("-123", `isinteger`) // =>
validation.Validate("12.3", `isinteger`) // => an error
validation.Validate("abc", `isinteger`) // => an error// Validate whether an string is an float string that can be parsed to an float.
validation.Validate("123", `isnumber`) // =>
validation.Validate("12.3", `isnumber`) // =>
validation.Validate("-1.2", `isnumber`) // =>
validation.Validate("123.", `isnumber`) // =>
validation.Validate(".123", `isnumber`) // =>
validation.Validate("abc", `isnumber`) // => an error// Validate whether a value is ZERO.
validation.Validate(0, `zero`) // =>
validation.Validate(1, `zero`) // => an error
validation.Validate("", `zero`) // =>
validation.Validate("0", `zero`) // => an error
validation.Validate(false, `zero`) // =>
validation.Validate(true, `zero`) // => an error
var p *int
validation.Validate(p, `zero`) // =>
p = new(int)
validation.Validate(p, `zero`) // => an error// Validate whether a value is not ZERO.
validation.Validate(0, `required`) // => an error
validation.Validate(1, `required`) // =>
validation.Validate("", `required`) // => an error
validation.Validate("0", `required`) // =>
validation.Validate(false, `required`) // => an error
validation.Validate(true, `required`) // =>
p = nil
validation.Validate(p, `required`) // => an error
p = new(int)
validation.Validate(p, `required`) // =>// Validate whether a slice/array is valid.
validation.Validate([]int{1, 2}, `array(min(1), max(10))`) // slice, =>
validation.Validate([3]int{1, 2}, `array(min(1), max(10))`) // array, => an error// Validate whether a map(key, value or key-value) is valid.
maps := map[int]string{0: "a", 1: "b"}
validation.Validate(maps, `mapk(min(0), max(10))`) // =>
validation.Validate(maps, `mapk(min(1), max(10))`) // => an error
validation.Validate(maps, `mapv(oneof("a", "b"))`) // =>
validation.Validate(maps, `mapv(oneof("a", "c"))`) // => an error// For the validation rule, it support the multi-level AND/OR. For example,
// - "min(100) && max(200)"
// => A value, such as integer or length of string/slice, in [100, 200] is valid.
// - "min(200) || max(100)"
// => A value, such as integer or length of string/slice, in (-∞, 100] or [200, +∞) is valid.
// - "(min(200) || max(100)) && required)"
// => Same as "min(200) || max(100)", but also cannot be ZERO.
}
```