-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmain.go
49 lines (39 loc) · 995 Bytes
/
main.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
package main
import (
"fmt"
"reflect"
"strings"
"github.com/liangyaopei/checker"
)
type customStruct struct {
Inner struct {
Str string
}
}
func main() {
checkFn := func(exprValue interface{}) (bool, string) {
res, ok := exprValue.(string)
if !ok {
res = reflect.ValueOf(exprValue).String()
}
if !strings.Contains(res, "checker") {
return false, "String should contain checker."
}
return true, ""
}
rule := checker.Custom("Inner.Str", checkFn)
validator := checker.NewChecker()
validator.Add(rule, "wrong Custom")
custom := customStruct{}
custom.Inner.Str = "checker custom function"
isValid, _, _ := validator.Check(&custom)
fmt.Printf("isValid:%v", isValid)
fmt.Println()
custom.Inner.Str = "abc"
isValid, prompt, errMsg := validator.Check(&custom)
custom.Inner.Str = "checker custom function"
custom.Inner.Str = "abc"
isValid, prompt, errMsg = validator.Check(&custom)
fmt.Printf("prompt:%v\n", prompt)
fmt.Printf("errMsg:%v\n", errMsg)
}