-
Notifications
You must be signed in to change notification settings - Fork 34
/
validators_extra.go
127 lines (99 loc) · 2.91 KB
/
validators_extra.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package validation
import (
"fmt"
"net"
"reflect"
"strings"
"github.com/go-playground/validator/v10"
)
var ErrorExportInterface = fmt.Errorf("validators.extra_UniqueField: Cannot export private field!")
var ErrorFieldNotFound = fmt.Errorf("validators.extra_UniqueField: Field not found!")
// extra_RegexAny returns true if the field value matches any of the provided regex
// expressions.
func extra_RegexAny(fl validator.FieldLevel) bool {
for _, r := range strings.Split(fl.Param(), " ") {
if regex(r, fl.Field().String()) {
return true
}
}
return false
}
// extra_RegexAll returns true if the field value matches all of the provided regex
// expressions.
func extra_RegexAll(fl validator.FieldLevel) bool {
for _, r := range strings.Split(fl.Param(), " ") {
if !regex(r, fl.Field().String()) {
return false
}
}
return true
}
// extra_AlphaNumericDash checks whether the field contains only alphanumeric characters
// (a-Z0-9) and hyphen (-).
func extra_AlphaNumericHyphen(fl validator.FieldLevel) bool {
return regex("^[a-zA-Z0-9-]*$", fl.Field().String())
}
// extra_AlphaNumericDashUnderscore checks whether the field contains only alphanumeric
// characters (a-Z0-9), hyphen (-) and underscore (_).
func extra_AlphaNumericHyphenUnderscore(fl validator.FieldLevel) bool {
return regex("^[a-zA-Z0-9-_]*$", fl.Field().String())
}
// extra_VSemVer checks whether the field is a valid semantic version
// prefixed with 'v'.
func extra_VSemVer(fl validator.FieldLevel) bool {
return regex("^(v){1}(\\*|\\d+(\\.\\d+){2})$", fl.Field().String())
}
// extra_IPInRange checks whether the field is a valid IP within provided CIDR
func extra_IPInRange(fl validator.FieldLevel) bool {
_, subnet, err := net.ParseCIDR(fl.Param())
if err != nil {
return false
}
ip := net.ParseIP(fl.Field().String())
return subnet.Contains(ip)
}
// extra_UniqueField returns true if struct field with a given name is unique for
// all slice elements.
func extra_UniqueField(fl validator.FieldLevel) bool {
fName := string(fl.Param())
rv := getDeepValue(fl.Field())
if rv.Kind() != reflect.Slice {
return true
}
var fields []interface{}
for i := 0; i < rv.Len(); i++ {
ri := rv.Index(i)
if ri.Kind() != reflect.Struct {
return true
}
f := structFieldValue(ri, fName)
fields = append(fields, f)
}
for i := 0; i < len(fields); i++ {
for j := 0; j < len(fields); j++ {
if i != j && fields[i] == fields[j] {
return false
}
}
}
return true
}
// structFieldValue returns value of a struct field with a given name.
func structFieldValue(rs reflect.Value, fName string) interface{} {
for j := 0; j < rs.NumField(); j++ {
rf := rs.Field(j)
rfName := rs.Type().Field(j).Name
if rfName != fName {
continue
}
v := getDeepValue(rf)
if v.Kind() == reflect.Invalid {
return nil
}
if !v.CanInterface() {
panic(ErrorExportInterface)
}
return v.Interface()
}
panic(ErrorFieldNotFound)
}