After using validator, you only need to use it when defining the structurebinding
orvalidate
By identifying relevant verification rules for tags, you can perform parameter verification without having to write common verification rules alone.
package main import ( "fmt" "/go-playground/validator/v10" "/go-playground/locales/zh_Hans_CN" unTrans "/go-playground/universal-translator" zhTrans "/go-playground/validator/v10/translations/zh" ) type User struct { Username string `validate:"min=6,max=10,contains=ook,startswith=He"` Age uint8 `validate:"gte=1,lte=10"` Sex string `validate:"oneof=female male"` Email string `validate:"email"` } func main() { validate := () //user := User{Username: "Heooking", Age: 6, Sex: "male", Email: "test@"} user := User{Username: "Looking", Age: 26, Sex: "male", Email: ""} err := (user) // Default English prompt if err != nil { (err) //Key: '' Error:Field validation for 'Username' failed on the 'startswith' tag //Key: '' Error:Field validation for 'Age' failed on the 'lte' tag //Key: '' Error:Field validation for 'Email' failed on the 'email' tag } () // Chinese error message uni := (zh_Hans_CN.New()) trans, _ := ("zh_Hans_CN") (validate, trans) if err != nil { for _, v := range err.() { ((trans)) //Username must start with the text 'He' //Age must be less than or equal to 10 //Email must be a valid email address } } }
module test go 1.17 require ( /go-playground/validator/v10 v10.15.0 ) require ( /gabriel-vasile/mimetype v1.4.2 // indirect /go-playground/locales v0.14.1 // indirect /go-playground/universal-translator v0.18.1 // indirect /leodido/go-urn v1.2.4 // indirect /x/crypto v0.7.0 // indirect /x/net v0.8.0 // indirect /x/sys v0.6.0 // indirect )
Common constraints are as follows:
String constraints
- excludesall: does not contain any UNICODE characters in the parameter, such as excludesall=ab;
- excludesrune: does not contain the rune character represented by the parameter, excludesrune=asong;
- startswith: prefixed with parameter substring, for example startswith=hi;
- endswith: suffixed with parameter substrings, for example endswith=bye.
- contains=: contains parameter substrings, for example contains=email;
- containsany: contains any UNICODE character in the parameter, such as containsany=ab;
- containsrune: contains the rune character represented by the parameter, such as `containsrune=asong;
- excludes: does not contain parameter substrings, such as excludes=email;
Scope constraints
There are three types of field types for range constraints:
- For numeric values, we can constrain their values
- For slices, arrays, and maps, we can constrain their length
- For strings, we can constrain their length
Commonly used tag introduction:
- ne: does not equal the parameter value, for example, ne=5;
- gt: greater than the parameter value, for example gt=5;
- gte: greater than or equal to the parameter value, for example gte=50;
- lt: less than the parameter value, for example lt=50;
- lte: less than or equal to the parameter value, for example lte=50;
- oneof: can only be one of the listed values. These values must be numeric values or strings separated by spaces. If there are spaces in the string, the string is surrounded by single quotes, for example oneof=male female.
- eq: equal to the parameter value, note that it is different from len. For strings, eq constrains the value of the string itself, while len constrains the length of the string. For example, eq=10;
- len: equal to the parameter value, for example len=10;
- max: less than or equal to the parameter value, for example, max=10;
- min: greater than or equal to the parameter value, for example min=10
- Fields constraints
- eqfield: Defines equality constraints between fields to constrain fields in the same structure. For example: eqfield=Password
- eqcsfield: Constraint fields in a unified structure equal to another field (relative), and can be used when confirming the password, for example: eqfiel=ConfirmPassword
- nefield: Used to constrain whether the two fields are the same and can be used when confirming whether the two colors are the same, for example: nefield=Color1
- necsfield: Constraint whether two fields are the same (relative)
Common constraints
- unique: Specify uniqueness constraints, different types of processing are different:
For map, unique constraints have no duplicate values
For arrays and slices, unique has no duplicate values
For fragments with element type structure, a field in the unique constraint structure object is not repeated, and use unique=field to specify the field name.
- mail: Use gmail to restrict the fields to be in mail form, just write eamil directly, without adding any specifications.
- omitempty: field not set, it will be ignored
- -: Skip this field and do not check;
- |: Use multiple constraints, only one of them needs to be satisfied, such as rgb|rgba;
- required: The field must be set and cannot be the default value;
This is the article about Go's validator library verification. For more relevant Go validator library content, please search for my previous articles or continue browsing the following related articles. I hope everyone will support me in the future!