Repository navigation
Expand file tree
/
Copy pathsource_test.go
More file actions
117 lines (110 loc) · 4.02 KB
/
Copy pathsource_test.go
File metadata and controls
117 lines (110 loc) · 4.02 KB
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
package validator
import (
"context"
"net/url"
"strconv"
"testing"
)
// TestMapStringStringMatchesReflectPath pins the map[string]string fast path
// to the generic conversion: same lookups, same nested resolution, same
// verdicts.
func TestMapStringStringMatchesReflectPath(t *testing.T) {
ctx := context.Background()
rules := map[string]string{
"name": "required && alpha",
"email": "required && email",
"age": "required && numeric && gte:18",
}
fast := map[string]string{"name": "alice", "email": "a@b.com", "age": "20"}
type custom map[string]string
if err := MustMap(fast, rules).Validate(ctx); err != nil {
t.Fatalf("string map: %v", err)
}
if err := MustMap(custom(fast), rules).Validate(ctx); err != nil {
t.Fatalf("named string map: %v", err)
}
bad := map[string]string{"name": "1", "email": "nope", "age": "5"}
vd := MustMap(bad, rules)
if err := vd.Validate(ctx); err == nil || !vd.Errors().Has("name") || !vd.Errors().Has("email") || !vd.Errors().Has("age") {
t.Fatalf("string map failures not reported: %v", err)
}
// Nested string maps resolve dotted names through the same fast path.
nested := map[string]any{"user": map[string]string{"name": "bob", "zip": "abc"}}
vd = MustMap(nested, map[string]string{"user.name": "required && alpha", "user.zip": "required && numeric"})
if err := vd.Validate(ctx); err == nil || !vd.Errors().Has("user.zip") || vd.Errors().Has("user.name") {
t.Fatalf("nested string map: %v", err)
}
}
func TestValuesRepeatedKeyBecomesSlice(t *testing.T) {
ctx := context.Background()
form := url.Values{"Tags": {"go", "cron"}, "Name": {"alice"}, "Empty": {}}
vd := MustValues(form, map[string]string{
"Tags": "required && dive && alpha",
"Name": "required && alpha",
})
if err := vd.Validate(ctx); err != nil {
t.Fatalf("repeated key should validate element-wise: %v", err)
}
var dst struct {
Tags []string
Name string
Empty string
}
if err := vd.Bind(&dst); err != nil {
t.Fatal(err)
}
if len(dst.Tags) != 2 || dst.Tags[0] != "go" || dst.Tags[1] != "cron" || dst.Name != "alice" || dst.Empty != "" {
t.Fatalf("bound = %+v", dst)
}
bad := MustValues(url.Values{"Tags": {"go", "c r0n"}}, map[string]string{"Tags": "dive && alpha"})
if err := bad.Validate(ctx); err == nil || !bad.Errors().Has("Tags[1]") {
t.Fatalf("second repeated value should fail alpha: %v", err)
}
}
// TestDynamicRulesKeepStaticRulesCached makes sure a stream of distinct rule
// strings neither corrupts nor starves the expression cache: every rule still
// validates correctly and the cache stays within its bound.
func TestDynamicRulesKeepStaticRulesCached(t *testing.T) {
ctx := context.Background()
v := MustNew()
for i := 0; i < 2*maxExprCache+10; i++ {
rule := "required && in:a,b,x" + strconv.Itoa(i)
vd := v.MustValue("a", rule)
if err := vd.Validate(ctx); err != nil {
t.Fatalf("rule %d: %v", i, err)
}
if i%1000 == 0 {
static := v.MustValue("nope", "required && email")
if err := static.Validate(ctx); err == nil {
t.Fatalf("static rule stopped failing at iteration %d", i)
}
}
}
if got := v.exprCache.len(); got > maxExprCache {
t.Fatalf("exprCache holds %d entries, bound is %d", got, maxExprCache)
}
if got := v.varPlans.len(); got > maxExprCache {
t.Fatalf("varPlans holds %d entries, bound is %d", got, maxExprCache)
}
}
func TestRegexRuleCompiledOncePerExpression(t *testing.T) {
ctx := context.Background()
v := MustNew()
for i, in := range []string{"abc123", "ABC", "xyz9"} {
vd := v.MustValue(in, `required && regex:"^[a-z]+[0-9]+$"`)
err := vd.Validate(ctx)
if want := i != 1; (err == nil) != want {
t.Fatalf("%q: err=%v want pass=%v", in, err, want)
}
}
if _, err := v.Value("x", `regex:"("`); err == nil {
t.Fatal("invalid pattern must be rejected when the expression is compiled")
}
not := v.MustValue("abc", `not_regex:"^[0-9]+$"`)
if err := not.Validate(ctx); err != nil {
t.Fatalf("not_regex: %v", err)
}
if err := v.MustValue("123", `not_regex:"^[0-9]+$"`).Validate(ctx); err == nil {
t.Fatal("not_regex should fail on a match")
}
}