-
Notifications
You must be signed in to change notification settings - Fork 0
/
format_checker.go
115 lines (98 loc) · 2.99 KB
/
format_checker.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
package templatemap
import (
"fmt"
"regexp"
"strconv"
"github.com/xeipuuv/gojsonschema"
)
var (
rxPhone = regexp.MustCompile(`/^1[3456789]\d{9}$/`)
rxIdCard = regexp.MustCompile(`/^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/`)
rxIdCard1 = regexp.MustCompile(`/^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}$/`)
rxPostCode = regexp.MustCompile(`/^[1-9]{1}(\d+){5}$/`)
)
func RegisterFormatChecker() {
gojsonschema.FormatCheckers.Add("number", NumberFormatChecker{}) // 数字格式验证
gojsonschema.FormatCheckers.Add("phone", NumberFormatChecker{}) // 数字格式验证
}
type NumberFormatChecker struct{}
// IsFormat checks if input is a correctly formatted number string
func (f NumberFormatChecker) IsFormat(input interface{}) bool {
asString, ok := input.(string)
if !ok {
return false
}
_, err := strconv.ParseFloat(asString, 64)
return err == nil
}
type PhoneFormatChecker struct{}
// IsFormat checks if input is a correctly formatted phone string
func (f PhoneFormatChecker) IsFormat(input interface{}) bool {
asString, ok := input.(string)
if !ok {
return false
}
out := rxPhone.MatchString(asString)
return out
}
type IDCardFormatChecker struct{}
// IsFormat checks if input is a correctly formatted IDCard string
func (f IDCardFormatChecker) IsFormat(input interface{}) bool {
asString, ok := input.(string)
if !ok {
return false
}
out := rxIdCard.MatchString(asString) || rxIdCard1.MatchString(asString)
return out
}
type PostCodeFormatChecker struct{}
// IsFormat checks if input is a correctly formatted postcode string
func (f PostCodeFormatChecker) IsFormat(input interface{}) bool {
asString, ok := input.(string)
if !ok {
return false
}
out := rxPostCode.MatchString(asString)
return out
}
// 数据库验证(这个需要调用时重新注册,方便更新TplName 等数据)
type ValidDBChecker struct {
Repository RepositoryInterface
TplName string
Volume VolumeInterface
}
func (f *ValidDBChecker) Name() string {
return "DBValidate"
}
func (f *ValidDBChecker) IsFormat(input interface{}) bool {
out, err := f.Repository.ExecuteTemplate(f.TplName, f.Volume)
if err != nil {
panic(err)
}
outputKey := fmt.Sprintf("%sOut", f.TplName)
f.Volume.SetValue(outputKey, out)
okKey := fmt.Sprintf("%sOut.ok", f.TplName)
var ok bool
f.Volume.GetValue(okKey, &ok)
return ok
}
func (f *ValidDBChecker) Msg() string {
msgKey := fmt.Sprintf("%sOut.msg", f.TplName)
var msg string
f.Volume.GetValue(msgKey, &msg)
return msg
}
func TransferWithValidate(tplName string, volume VolumeInterface, transferPaths TransferPaths, jsonSchema string) (string, error) {
out, err := TransferDataFromVolume(volume, transferPaths)
if err != nil {
return "", err
}
// 暂时注释验证,客户端输入,必须在之前验证,内部函数参数,暂时不验证
// if jsonSchema != "" {
// err = Validate(out, jsonSchema)
// if err != nil {
// return "", err
// }
// }
return out, nil
}