github.com/bytedance/go-tagexpr/v2@v2.9.8/validator/example_test.go (about) 1 package validator_test 2 3 import ( 4 "fmt" 5 6 vd "github.com/bytedance/go-tagexpr/v2/validator" 7 ) 8 9 func Example() { 10 type InfoRequest struct { 11 Name string `vd:"($!='Alice'||(Age)$==18) && regexp('\\w')"` 12 Age int `vd:"$>0"` 13 Email string `vd:"email($)"` 14 Phone1 string `vd:"phone($)"` 15 OtherPhones []string `vd:"range($, phone(#v,'CN'))"` 16 *InfoRequest `vd:"?"` 17 Info1 *InfoRequest `vd:"?"` 18 Info2 *InfoRequest `vd:"-"` 19 } 20 info := &InfoRequest{ 21 Name: "Alice", 22 Age: 18, 23 Email: "henrylee2cn@gmail.com", 24 Phone1: "+8618812345678", 25 OtherPhones: []string{"18812345679", "18812345680"}, 26 } 27 fmt.Println(vd.Validate(info)) 28 29 type A struct { 30 A int `vd:"$<0||$>=100"` 31 Info interface{} 32 } 33 info.Email = "xxx" 34 a := &A{A: 107, Info: info} 35 fmt.Println(vd.Validate(a)) 36 type B struct { 37 B string `vd:"len($)>1 && regexp('^\\w*$')"` 38 } 39 b := &B{"abc"} 40 fmt.Println(vd.Validate(b) == nil) 41 42 type C struct { 43 C bool `vd:"@:(S.A)$>0 && !$; msg:'C must be false when S.A>0'"` 44 S *A 45 } 46 c := &C{C: true, S: a} 47 fmt.Println(vd.Validate(c)) 48 49 type D struct { 50 d []string `vd:"@:len($)>0 && $[0]=='D'; msg:sprintf('invalid d: %v',$)"` 51 } 52 d := &D{d: []string{"x", "y"}} 53 fmt.Println(vd.Validate(d)) 54 55 type E struct { 56 e map[string]int `vd:"len($)==$['len']"` 57 } 58 e := &E{map[string]int{"len": 2}} 59 fmt.Println(vd.Validate(e)) 60 61 // Customizes the factory of validation error. 62 vd.SetErrorFactory(func(failPath, msg string) error { 63 return fmt.Errorf(`{"succ":false, "error":"validation failed: %s"}`, failPath) 64 }) 65 66 type F struct { 67 f struct { 68 g int `vd:"$%3==0"` 69 } 70 } 71 f := &F{} 72 f.f.g = 10 73 fmt.Println(vd.Validate(f)) 74 75 fmt.Println(vd.Validate(map[string]*F{"a": f})) 76 fmt.Println(vd.Validate(map[string]map[string]*F{"a": {"b": f}})) 77 fmt.Println(vd.Validate([]map[string]*F{{"a": f}})) 78 fmt.Println(vd.Validate(struct { 79 A []map[string]*F 80 }{A: []map[string]*F{{"x": f}}})) 81 fmt.Println(vd.Validate(map[*F]int{f: 1})) 82 fmt.Println(vd.Validate([][1]*F{{f}})) 83 fmt.Println(vd.Validate((*F)(nil))) 84 fmt.Println(vd.Validate(map[string]*F{})) 85 fmt.Println(vd.Validate(map[string]map[string]*F{})) 86 fmt.Println(vd.Validate([]map[string]*F{})) 87 fmt.Println(vd.Validate([]*F{})) 88 89 // Output: 90 // <nil> 91 // email format is incorrect 92 // true 93 // C must be false when S.A>0 94 // invalid d: [x y] 95 // invalid parameter: e 96 // {"succ":false, "error":"validation failed: f.g"} 97 // {"succ":false, "error":"validation failed: {v for k=a}.f.g"} 98 // {"succ":false, "error":"validation failed: {v for k=a}{v for k=b}.f.g"} 99 // {"succ":false, "error":"validation failed: [0]{v for k=a}.f.g"} 100 // {"succ":false, "error":"validation failed: A[0]{v for k=x}.f.g"} 101 // {"succ":false, "error":"validation failed: {k}.f.g"} 102 // {"succ":false, "error":"validation failed: [0][0].f.g"} 103 // unsupport data: nil 104 // <nil> 105 // <nil> 106 // <nil> 107 // <nil> 108 }