This is inspired by Checkit that written in JS. You can check out the original lib below
https://github.com/tgriesser/checkit
I'm interested in Golang while I'm doing a blockchain platform project. My responsibility is contributing a RESTFul module to communicate with the core blockchain
I wrote this lib with the first idea that it would help me validate request body.
It allows you to seamlessly validate full Golang structs, primitive type values, defining custom messages, labels, and validations.
You just open Terminal
and run this command
go get https://github.com/dungntm58/checkit
to inject it as a dependency
r, err := Validator(map[string]Validating{
"a": Between(0, 2),
"b": MaxLength(2),
"c": ExactLength(3),
}).ValidateSync(struct {
a int
b *string
c []interface{}
}{
a: 1,
b: &str,
c: []interface{}{0, 1, 2},
})
fmt.Println(r) // true
r, err := Integer().Validate(1)
fmt.Println(r) // true
Validation Name | Description |
---|---|
Accepted | The value must be yes, on, or 1. This is useful for validating "Terms of Service" acceptance. |
Alpha | The value must be entirely alphabetic characters. |
AlphaDash | The value may have alpha-numeric characters, as well as dashes and underscores. |
AlphaNumeric | The value must be entirely alpha-numeric characters. |
AlphaUnderscore | The value must be entirely alpha-numeric, with underscores but not dashes. |
Array | The value must be a valid array object. |
Base64 | The value must be a base64 encoded value. |
Between:min:max | The value must have a size between the given min and max. |
Boolean | The value must be a javascript boolean. |
Contains:value | The value must be a string or an array and contain the value. |
Date | The value must be a valid date object. |
The field must be a valid formatted e-mail address. | |
Empty | The value under validation must be empty; either an empty string, an empty, array, empty object, or a falsy value. |
ExactLength:value | The field must have the exact length of "val". |
ExistsNonNil | The value under validation must be exist or not nil. |
Finite | The value under validation must be a finite number. |
Function | The value under validation must be a function. |
GreaterThan:value | The value under validation must be "greater than" the given value. |
GreaterThanEqualTo:value | The value under validation must be "greater than" or "equal to" the given value. |
Integer | The value must have an integer value. |
Ipv4 | The value must be formatted as an IPv4 address. |
Ipv6 | The value must be formatted as an IPv6 address. |
LessThan:value | The value must be "less than" the specified value. |
LessThanEqualTo:value | The value must be "less than" or "equal to" the specified value. |
Luhn | The given value must pass a basic luhn (credit card) check regular expression. |
Max:value | The value must be less than a maximum value. Strings, numerics, and files are evaluated in the same fashion as the size rule. |
MaxLength:value | The value must have a length property which is less than or equal to the specified value. Note, this may be used with both arrays and strings. |
Min:value | The value must have a minimum value. Strings, numerics, and files are evaluated in the same fashion as the size rule. |
MinLength:value | The value must have a length property which is greater than or equal to the specified value. Note, this may be used with both arrays and strings. |
NaN | The value must be NaN. |
Natural | The value must be a natural number (a number greater than or equal to 0). |
NaturalNonZero | The value must be a natural number, greater than or equal to 1. |
Object | The value must be anything except functions, pointers. |
PlainObject | The value must be a map. |
Regex | The value must be a Go RegExp object. |
String | The value must be a string type. |
URL | The value must be formatted as an URL. |
UUID | Passes for a validly formatted UUID. |