validating
A Go library for validating structs, maps and slices.
Features
-
Simple
Simple and stupid, no magic involved.
-
Type-safe
Schema is defined in Go, which is type-safer (and more powerful) than traditional struct tags.
-
Flexible
- Validators are composable.
- Nested struct validation is well supported.
- Schema can be defined inside or outside struct.
- Validator customizations are made easy.
-
No reflection
Installation
$ go get github.com/RussellLuo/validating/v3@latest
Validator factories and validators
To be strict, this library has a conceptual distinction between validator factory
and validator
.
A validator factory is a function used to create a validator, which will do the actual validation.
Built-in validator factories
- Func
- Schema
- Value
- Nested
- Map
- Slice/Array
- All/And
- Any/Or
- Not
- Is
- Nonzero
- Zero
- ZeroOr
- LenString
- LenSlice
- RuneCount
- Eq
- Ne
- Gt
- Gte
- Lt
- Lte
- Range
- In
- Nin
- Match
Extension validator factories
Validator customizations
Examples
- Simple value
- Simple string (is IP?)
- Simple struct
- Simple slice
- Simple map
- Nested struct
- Nested struct (schema inside)
- Nested struct pointer
- Nested struct slice
- Nested struct map
Documentation
Check out the Godoc.
Thanks
This library borrows some ideas from the following libraries:
-
Prefer no reflection.
-
Support composite validator factories
All
/And
,Any
/Or
. -
Use the term
nonzero
instead ofrequired
/optional
.