Chekku is Golang validation library
- Table of Contents
- Examples
- Available Rules
required
either
isAlpha
isAlphanumeric
isArray
isBase64
isBool
isDate
isEmail
isFloat
isIn
isInt
isIPv4
isIPv6
isISBN
isLatitude
isLongitude
isNumber
isNumeric
isUUID
lengthBetween:min,max
maxLength:max
minLength:min
numberBetween:min,max
maxNumber:max
minNumber:min
notIn
requiredIf
requiredUnless
startsWith:str
endWith:str
mimetype:value
filesize:value
dimensions:value
go get github.com/KodepandaID/chekku
package main
import "github.com/KodepandaID/chekku"
type Payload struct {
Name string `chekku:"required"`
Age string `chekku:"required|isNumber"`
}
inputs := Payload{
Name: "",
Age: "20",
}
e := chekku.Validate(inputs)
if e != nil {
panic(e)
}
Use |
as separators to use multiple validations at once.
in version 1 the return error is converted to an array struct as below:
type Error struct {
Code string
Detail string
}
If you want to set error message by yourself, you can use tag errors
on your struct.
Example:
type Error struct {
Username string `chekku:"required|maxLength:10" errors:"required:username tidak boleh kosong|maxLength:Panjang username tidak boleh lebih dari 10 karakter"`
}
e := chekku.Validate(Error{
Username: "",
})
if e == nil {
panic(e)
}
Value under this field should not be nil
or an empty string (""
).
- Invalid values:
nil
,""
,0
,false
,[]string{}
,[]int{}
,[]float{}
- Valid values:
"not empty string"
,0.1
,[]string{""}
Use this as an OR
operator.
For example you may want to accept ipv4 or ipv6, you can use either rule like below:
Example:
type IP struct {
ip string `chekku:"either:isIPv4,isIPv6"`
}
validValues := []string{
"1.2.3.4", // Valid
"::1", // Valid
"not an IP Address" // Invalid
}
for _, v := range validValues {
e := chekku.Validate(IP{
ip: v,
})
if e != nil {
panic(e)
}
}
Value under this field must be the words Aa - Zz.
- Invalid values:
"123"
,"hello123"
, etc. - Valid values:
"hello"
,"Hello"
, etc.
Value under this field must be the words or numbers.
- Invalid values:
"hello@mail.com"
,"Here!!!"
,"~@#$%^&*()"
,123
, etc. - Valid values:
"123"
,"hello"
,"hello123"
, etc.
Value under this field must be an array.
- Invalid values:
""
,10
,0.5
, etc. - Valid values:
[]string{}
,map[string]string{}
, etc.
Value under this field must be a base64.
- Invalid values:
"123"
,0.5
,"aGVsbG%8gd29ybGQ="
, etc. - Valid values:
"aGVsbG8gd29ybGQ="
, etc.
Value under this field must be a boolean.
- Invalid values:
"true"
,1
, etc. - Valid values:
true
,false
.
Value under this field must be a Date format.
- Invalid values:
"2020"
,"01-2020-10"
,"2020-01"
, etc. - valid values:
"2020-01-02"
,"2020-01-02 23:59:59"
,"02/12/2020"
,"2020-January-20"
, etc.
Value under this field mus be an email address.
- Invalid values:
123
,"hello"
, etc. - Valid values:
"hello@kodepanda.com"
, etc.
Value under this field must be a float.
- Invalid values:
1
,"hello"
, etc. - Valid values:
0.123
, etc.
Value under this field must be one of allowed values
Example:
type Payload struct {
text1 string `chekku:"isIn:yes,no"`
text2 string `chekku:"isIn:yes,no"`
text3 string `chekku:"isIn:yes,no"`
}
p := Payload{
text1: "yes", // passes
text2: "no", // passes
text3: "maybe", // fail
}
e := chekku.Validate(p)
if e != nil {
panic(e)
}
Value under this field must be an integer.
- Invalid values:
0.5
,"123"
, etc. - Valid values:
0
,123
, etc.
Value under this field must be IP Address version 4.
- Invalid values:
123
,"1.2.3.a"
,"1.2.3.256"
,"FEDC:BA98:7654:3210:FEDC:BA98:7654:3210"
etc. - Valid values:
"0.1.2.3"
,"255.255.255.255"
, etc.
Value under this field must be IP Address version 6.
- Invalid values:
"2001:db8::7"
,"::1:2:3:4:5:6:7"
, etc. - Valid values:
"2001:db8::7"
,"::1:2:3:4:5:6:7"
, etc.
Value under this field must be an ISBN 10 or ISBN 13.
- Invalid values:
"123"
,"ISBX 979-1-23456-789-6"
, etc. - Valid values:
"ISBN 979-1-23456-789-6"
,"ISBN 1-23456-789-X"
, etc.
Value under this field must be a latitude geolocation.
- Invalid values:
"+90.0123"
,"123"
,"-123"
, etc. - Valid values:
+90.0123
,123
,-123
, etc.
Value under this field must be a longitude geolocation.
- Invalid values:
"182.1234"
,"-181.1234"
,182.1234
,-181.1234
etc. - Valid values:
106.900447
,-120.1234
,+90.123
, etc.
Value under this value must be a number.
- Invalid values:
"hello"
,"123"
, etc. - Valid values:
123
,1.23
, etc.
Value under this field must be a numeric.
- Invalid values:
"hello"
, etc. - Valid values:
"123"
,"1.23"
,123
,1.23
, etc.
Value under this field must be an UUID.
- Invalid values:
"hello"
,"123-456-789"
, etc. - Valid values:
"17b32300-f33c-11ea-adc1-0242ac120002"
, etc.
Value under this field must be a string that has char length between min
and max
.
Example:
type Text struct {
text1 string `chekku:"lengthBetween:5,10"`
text2 string `chekku:"lengthBetween:5,10"`
text3 string `chekku:"lengthBetween:5,10"`
}
p := Text{
text1: "hell", // fail
text2: "hello world, how are you?", // fail
text3: "hello" // passes
}
e := chekku.Validate(p)
if e != nil {
panic(e)
}
Value under this field must be a string that has char length lower or equals max
.
Example:
type Text struct {
text1 string `chekku:"maxLength:5"`
text2 string `chekku:"maxLength:5"`
}
p := Text{
text1: "hello", // passes
text2:"hello world" // fail
}
e := chekku.Validate(p)
if e != nil {
panic(e)
}
Value under this field must be a string that has char length higher or equals min
.
Example:
type Text struct {
text1 string `chekku:"minLength:5"`
text2 string `chekku:"minLength:5"`
}
p := Text{
text1: "hell", // fail
text2:"hello world" // passes
}
e := chekku.Validate(p)
if e != nil {
panic(e)
}
Value under this field must be a int or float that has value between min
and max
.
Example:
type Number struct {
number1 int `chekku:"numberBetween:5,10"`
number2 int `chekku:"numberBetween:5,10"`
number3 float64 `chekku:"numberBetween:5.1,5.3"`
}
p := Number{
number1: 4, // fail
number2: 11, // fail
number3: 5.2 // passes
}
e := chekku.Validate(p)
if e != nil {
panic(e)
}
Value under this field must be a int or float that has value lower or equals max
.
Example:
type Number struct {
number1 int `chekku:"maxNumber:5"`
number2 float64 `chekku:"maxNumber:5"`
}
p := Number{
number1: 4, // passes
number2: 5.1 // fail
}
e := chekku.Validate(p)
if e != nil {
panic(e)
}
Value under this field must be a int or float that has value higher or equals min
.
Example:
type Number struct {
number1 int `chekku:"minNumber:5"`
number2 int `chekku:"minNumber:5"`
}
p := Number{
number1: 4, // fail
number2: 6 // passes
}
e := chekku.Validate(p)
if e != nil {
panic(e)
}
Field within this rule will be required if given anotherField
match value
.
Example Passes (text1 is optional because text0 value not match):
type Text struct {
text0 int
text1 string `chekku:"requiredIf:text0,200"`
}
e := chekku.Validate(Text{
text0: 201,
})
if e != nil {
panic(e)
}
Example Fail (text1 is required because text0 value is a match):
type Text struct {
text0 int
text1 string `chekku:"requiredIf:text0,200"`
}
e := chekku.Validate(Text{
text0: 200,
})
if e != nil {
panic(e)
}
Field within this rule will be required if given anotherField
doesn't match with value
.
Example Passes (text1 is optional because text0 value is a match):
type Text struct {
text0 int
text1 string `chekku:"requiredUnless:text0,200"`
}
e := chekku.Validate(Text{
text0: 200,
})
if e != nil {
panic(e)
}
Example Fail (text1 is required because text0 value doesn't match):
type Text struct {
text0 int
text1 string `chekku:"requiredUnless:text0,200"`
}
e := chekku.Validate(Text{
text0: 201,
})
if e != nil {
panic(e)
}
Value under this field must be a string that starts with prefix str
.
Example:
type Text struct {
text1 string `chekku:"startsWith:foo"`
text2 string `chekku:"startsWith:foo"`
text3 string `chekku:"startsWith:foo"`
}
p := Text{
text1: "", // fail
text2: "barfoo", // fail
text3: "foobar", // passes
}
e := chekku.Validate(p)
if e != nil {
panic(e)
}
Value under this field must be a string that end with prefix str
.
Example:
type Text struct {
text1 string `chekku:"endWith:foo"`
text2 string `chekku:"endWith:foo"`
text3 string `chekku:"endWith:foo"`
}
p := Text{
text1: "", // fail
text2: "barfoo", // passes
text3: "foobar", // fail
}
e := chekku.Validate(p)
if e != nil {
panic(e)
}
The file under validation must match one of the given MIME types:
Example:
type Request struct {
File *multipart.FileHeader `chekku:"mimetype:image/png"`
}
// fh is a variable to pass multipart.FileHeader you can use another name whatever you want.
e = chekku.Validate(Request{
File: fh,
})
if e != nil {
panic(e)
}
A full listing of MIME types and their corresponding extensions may be found at the following location: https://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types
The file under validation cannot be exceeded max size in Kilobytes
.
Example:
type Request struct {
File *multipart.FileHeader `chekku:"filesize:512"`
}
// fh is a variable to pass multipart.FileHeader you can use another name whatever you want.
e = chekku.Validate(Request{
File: fh,
})
if e != nil {
panic(e)
}
The file under validation must be an image meeting the dimension constraints as specified by the rule's parameters.
Available constraints are: min_width, max_width, min_height, max_height, width, height, ratio.
A ratio constraint should be represented as width divided by height. This can be specified either by a statement like 1/1, 4/3, 16/9 or etc.
Example:
type Request struct {
File *multipart.FileHeader `chekku:"dimensions:max_width=300,min_width=150,max_height=300,min_height=150,ratio=1/1"`
}
// fh is a variable to pass multipart.FileHeader you can use another name whatever you want.
e = chekku.Validate(Request{
File: fh,
})
if e != nil {
panic(e)
}