Vali, a purposefully tiny validator. Some 🔋🔋🔋 included,
plus recipes on how to make more of them
Vali aims for the most things you could do with a minimal set of checks, therefore it has a small set of checks and an easy way to add your own checks, see the example and vali_test.go files.
You can also change the struct tag name being used (by creating
a new Validator) and a few other bits, see Validator type
definition.
It is pointer-insensitive, will always validate the value behind the pointer, that is, given:
Foo *string `validate:"required"`passes if *Foo != "" NOT if Foo != nil.
It validates both public and private fields, as long as they have the validation tags.
Non-goals:
slice/mapdive;- cross field checks;
- anything that needs a 3rd party dep.
Why? Complex validation reads better when is expressed as Go code, rather than in struct tags "perlisms".
| Check | Description | Domain |
|---|---|---|
| required | must NOT be IsZero() |
any |
regex:<rx> |
must match <rx> |
string, Stringer |
eq:<number> |
must == number |
CanInt, CanUint, CanFloat, CanLen |
ne:<number> |
must != number |
same as eq |
min:<number> |
must be >= number |
same as eq |
max:<number> |
must be <= number |
same as eq |
| one_of:a|b|c | must be one of {a,b,c} | same as regex |
| uuid | 32 (dash separated) hexdigits | same as regex |
| valid email address | string, Stringer |
|
| url | valid URL with scheme and host | string, Stringer |
| ipv4 | valid IPv4 address | string, Stringer |
| ipv6 | valid IPv6 address | string, Stringer |
| ip | valid IP address (v4 or v6) | string, Stringer |
| mac | valid MAC address | string, Stringer |
| domain | valid domain name | same as regex |
| isbn | valid ISBN-10 or ISBN-13 | string, Stringer |
| alpha | letters only | same as regex |
| alphanum | letters and numbers only | same as regex |
| numeric | numbers only | same as regex |
| boolean | valid boolean representation | string, Stringer, numeric |
| creditcard | valid credit card number | string, Stringer, numeric |
| json | valid JSON format | string, Stringer, numeric |
| ascii | ASCII characters only | string, Stringer |
| lowercase | lowercase characters only | string, Stringer |
| uppercase | uppercase characters only | string, Stringer |
| hexadecimal | valid hexadecimal string | same as regex |
| base64 | valid base64 string | same as regex |
| mongoid | valid MongoDB ObjectID | same as regex |
| rgb | valid RGB color | same as regex |
| rgba | valid RGBA color | same as regex |
| luhn | valid luhn string or number | string, Stringer, numeric |
| ssn | valid Social Security Number | same as regex |
| npi | valid NPI number | string, Stringer, numeric |
<your_own> |
you can easily add your own... | ... |
Multiple checks must be combined with a comma (,) extra space
is forgiven, and empty checks are ignored i.e.:
validate:"required,,,, uuid , one_of:foo|bar|baz" is fine, albeit unclean.
Both separators (between checks and between a check and its arguments)
are configurable, whereas the separator between a check's arguments (the
pipe symbol in the a|b|c example above) are up the each individual checker,
the library doesn't care, it will just pass all the arguments as a string
to the Checker func.
s := struct {
Foo struct {
Bar string `validate:"required, one_of:foo|bar|baz"`
}
email string `validate:"required,email"`
}{}
if err := vali.Validate(s); err != nil {
fmt.Println("oh noes!...")
}- this README;
- the example file;
- the code documentation and
- the tests.