Skip to content

Commit 5bbca66

Browse files
author
Dean Karn
authored
Merge pull request #530 from skateinmars/chore/improve-doc
Improve documentation for custom functions
2 parents 17c1fa8 + c370ccf commit 5bbca66

File tree

3 files changed

+43
-28
lines changed

3 files changed

+43
-28
lines changed

_examples/simple/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ func validateStruct() {
6868

6969
fmt.Println(err.Namespace())
7070
fmt.Println(err.Field())
71-
fmt.Println(err.StructNamespace()) // can differ when a custom TagNameFunc is registered or
72-
fmt.Println(err.StructField()) // by passing alt name to ReportError like below
71+
fmt.Println(err.StructNamespace())
72+
fmt.Println(err.StructField())
7373
fmt.Println(err.Tag())
7474
fmt.Println(err.ActualTag())
7575
fmt.Println(err.Kind())

_examples/struct-level/main.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package main
22

33
import (
44
"fmt"
5+
"reflect"
6+
"strings"
57

68
"gopkg.in/go-playground/validator.v9"
79
)
@@ -11,7 +13,7 @@ type User struct {
1113
FirstName string `json:"fname"`
1214
LastName string `json:"lname"`
1315
Age uint8 `validate:"gte=0,lte=130"`
14-
Email string `validate:"required,email"`
16+
Email string `json:"e-mail" validate:"required,email"`
1517
FavouriteColor string `validate:"hexcolor|rgb|rgba"`
1618
Addresses []*Address `validate:"required,dive,required"` // a person can have a home and cottage...
1719
}
@@ -31,6 +33,15 @@ func main() {
3133

3234
validate = validator.New()
3335

36+
// register function to get tag name from json tags.
37+
validate.RegisterTagNameFunc(func(fld reflect.StructField) string {
38+
name := strings.SplitN(fld.Tag.Get("json"), ",", 2)[0]
39+
if name == "-" {
40+
return ""
41+
}
42+
return name
43+
})
44+
3445
// register validation for 'User'
3546
// NOTE: only have to register a non-pointer type for 'User', validator
3647
// interanlly dereferences during it's type checks.
@@ -48,7 +59,7 @@ func main() {
4859
FirstName: "",
4960
LastName: "",
5061
Age: 45,
51-
Email: "Badger.Smith@gmail.com",
62+
Email: "Badger.Smith@gmail",
5263
FavouriteColor: "#000",
5364
Addresses: []*Address{address},
5465
}
@@ -67,10 +78,10 @@ func main() {
6778

6879
for _, err := range err.(validator.ValidationErrors) {
6980

70-
fmt.Println(err.Namespace())
71-
fmt.Println(err.Field())
72-
fmt.Println(err.StructNamespace()) // can differ when a custom TagNameFunc is registered or
73-
fmt.Println(err.StructField()) // by passing alt name to ReportError like below
81+
fmt.Println(err.Namespace()) // can differ when a custom TagNameFunc is registered or
82+
fmt.Println(err.Field()) // by passing alt name to ReportError like below
83+
fmt.Println(err.StructNamespace())
84+
fmt.Println(err.StructField())
7485
fmt.Println(err.Tag())
7586
fmt.Println(err.ActualTag())
7687
fmt.Println(err.Kind())
@@ -101,8 +112,8 @@ func UserStructLevelValidation(sl validator.StructLevel) {
101112
user := sl.Current().Interface().(User)
102113

103114
if len(user.FirstName) == 0 && len(user.LastName) == 0 {
104-
sl.ReportError(user.FirstName, "FirstName", "fname", "fnameorlname", "")
105-
sl.ReportError(user.LastName, "LastName", "lname", "fnameorlname", "")
115+
sl.ReportError(user.FirstName, "fname", "FirstName", "fnameorlname", "")
116+
sl.ReportError(user.LastName, "lname", "LastName", "fnameorlname", "")
106117
}
107118

108119
// plus can do more, even with different tag than "fnameorlname"

doc.go

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,27 +1058,14 @@ Validator notes:
10581058
And the best reason, you can submit a pull request and we can keep on
10591059
adding to the validation library of this package!
10601060
1061-
Panics
1062-
1063-
This package panics when bad input is provided, this is by design, bad code like
1064-
that should not make it to production.
1065-
1066-
type Test struct {
1067-
TestField string `validate:"nonexistantfunction=1"`
1068-
}
1069-
1070-
t := &Test{
1071-
TestField: "Test"
1072-
}
1073-
1074-
validate.Struct(t) // this will panic
1075-
10761061
Non standard validators
10771062
10781063
A collection of validation rules that are frequently needed but are more
10791064
complex than the ones found in the baked in validators.
1080-
A non standard validator must be registered manually using any tag you like.
1081-
See below examples of registration and use.
1065+
A non standard validator must be registered manually like you would
1066+
with your own custom validation functions.
1067+
1068+
Example of registration and use:
10821069
10831070
type Test struct {
10841071
TestField string `validate:"yourtag"`
@@ -1089,13 +1076,30 @@ See below examples of registration and use.
10891076
}
10901077
10911078
validate := validator.New()
1092-
validate.RegisterValidation("yourtag", validations.ValidatorName)
1079+
validate.RegisterValidation("yourtag", validators.NotBlank)
1080+
1081+
Here is a list of the current non standard validators:
10931082
10941083
NotBlank
10951084
This validates that the value is not blank or with length zero.
10961085
For strings ensures they do not contain only spaces. For channels, maps, slices and arrays
10971086
ensures they don't have zero length. For others, a non empty value is required.
10981087
10991088
Usage: notblank
1089+
1090+
Panics
1091+
1092+
This package panics when bad input is provided, this is by design, bad code like
1093+
that should not make it to production.
1094+
1095+
type Test struct {
1096+
TestField string `validate:"nonexistantfunction=1"`
1097+
}
1098+
1099+
t := &Test{
1100+
TestField: "Test"
1101+
}
1102+
1103+
validate.Struct(t) // this will panic
11001104
*/
11011105
package validator

0 commit comments

Comments
 (0)