forked from bytedance/go-tagexpr
/
func.go
124 lines (115 loc) · 3.15 KB
/
func.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
package validator
import (
"errors"
"fmt"
"regexp"
"github.com/nyaruka/phonenumbers"
tagexpr "github.com/21888/go-tagexpr-new/v2"
)
// ErrInvalidWithoutMsg verification error without error message.
var ErrInvalidWithoutMsg = errors.New("")
// MustRegFunc registers validator function expression.
// NOTE:
//
// panic if exist error;
// example: phone($) or phone($,'CN');
// If @force=true, allow to cover the existed same @funcName;
// The go number types always are float64;
// The go string types always are string.
func MustRegFunc(funcName string, fn func(args ...interface{}) error, force ...bool) {
err := RegFunc(funcName, fn, force...)
if err != nil {
panic(err)
}
}
// RegFunc registers validator function expression.
// NOTE:
//
// example: phone($) or phone($,'CN');
// If @force=true, allow to cover the existed same @funcName;
// The go number types always are float64;
// The go string types always are string.
func RegFunc(funcName string, fn func(args ...interface{}) error, force ...bool) error {
return tagexpr.RegFunc(funcName, func(args ...interface{}) interface{} {
err := fn(args...)
if err == nil {
// nil defaults to false, so returns true
return true
}
return err
}, force...)
}
func init() {
var pattern = "^([A-Za-z0-9_\\-\\.\u4e00-\u9fa5])+\\@([A-Za-z0-9_\\-\\.])+\\.([A-Za-z]{2,8})$"
emailRegexp := regexp.MustCompile(pattern)
MustRegFunc("email", func(args ...interface{}) error {
if len(args) != 1 {
return errors.New("number of parameters of email function is not one")
}
s, ok := args[0].(string)
if !ok {
return errors.New("parameter of email function is not string type")
}
matched := emailRegexp.MatchString(s)
if !matched {
// return ErrInvalidWithoutMsg
return errors.New("email format is incorrect")
}
return nil
}, true)
}
func init() {
// phone: defaultRegion is 'CN'
MustRegFunc("phone", func(args ...interface{}) error {
var numberToParse, defaultRegion string
var ok bool
switch len(args) {
default:
return errors.New("the number of parameters of phone function is not one or two")
case 2:
defaultRegion, ok = args[1].(string)
if !ok {
return errors.New("the 2nd parameter of phone function is not string type")
}
fallthrough
case 1:
numberToParse, ok = args[0].(string)
if !ok {
return errors.New("the 1st parameter of phone function is not string type")
}
}
if defaultRegion == "" {
defaultRegion = "CN"
}
num, err := phonenumbers.Parse(numberToParse, defaultRegion)
if err != nil {
return err
}
matched := phonenumbers.IsValidNumber(num)
if !matched {
// return ErrInvalidWithoutMsg
return errors.New("phone format is incorrect")
}
return nil
}, true)
}
func init() {
// in: Check if the first parameter is one of the enumerated parameters
MustRegFunc("in", func(args ...interface{}) error {
switch len(args) {
case 0:
return nil
case 1:
return errors.New("input parameters of the in function are at least two")
default:
elem := args[0]
set := args[1:]
for _, e := range set {
if elem == e {
return nil
}
}
return fmt.Errorf("%#v is not in the list %+v", elem, set)
}
}, true)
}