-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate_callback_test.go
More file actions
151 lines (146 loc) · 3.79 KB
/
validate_callback_test.go
File metadata and controls
151 lines (146 loc) · 3.79 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* Copyright (c) 2024-2026 Mikhail Knyazhev <markus621@yandex.com>. All rights reserved.
* Use of this source code is governed by a BSD 3-Clause license that can be found in the LICENSE file.
*/
package validate
import (
"context"
"errors"
"fmt"
"testing"
)
func TestValidator_Validate(t *testing.T) {
ctx := context.Background()
tests := []struct {
name string
rules []Rule
callback func(c Callback)
wantErr bool
errString string
}{
{
name: "require success",
rules: []Rule{
{Name: "positive", Handle: HandlerFunc(func(ctx context.Context, value any, opts ...any) error {
if v, ok := value.(int); ok && v > 0 {
return nil
}
return errors.New("must be positive")
})},
},
callback: func(c Callback) {
c.Require("positive", 42)
},
wantErr: false,
},
{
name: "require failure",
rules: []Rule{
{Name: "positive", Handle: HandlerFunc(func(ctx context.Context, value any, opts ...any) error {
return errors.New("value not positive")
})},
},
callback: func(c Callback) {
c.Require("positive", -1)
},
wantErr: true,
},
{
name: "optional zero value skipped",
rules: []Rule{
{Name: "positive", Handle: HandlerFunc(func(ctx context.Context, value any, opts ...any) error {
return errors.New("should not be called")
})},
},
callback: func(c Callback) {
c.Optional("positive", 0)
},
wantErr: false,
},
{
name: "optional non-zero called",
rules: []Rule{
{Name: "positive", Handle: HandlerFunc(func(ctx context.Context, value any, opts ...any) error {
if v, ok := value.(int); ok && v > 0 {
return nil
}
return errors.New("must be positive")
})},
},
callback: func(c Callback) {
c.Optional("positive", 5)
},
wantErr: false,
},
{
name: "rule not found",
rules: []Rule{
{Name: "exists", Handle: HandlerFunc(func(ctx context.Context, value any, opts ...any) error { return nil })},
},
callback: func(c Callback) {
c.Require("unknown", "val")
},
wantErr: true,
errString: "validator `unknown` not found",
},
{
name: "multiple calls, first fails",
rules: []Rule{
{Name: "alpha", Handle: HandlerFunc(func(ctx context.Context, value any, opts ...any) error {
return errors.New("alpha error")
})},
{Name: "beta", Handle: HandlerFunc(func(ctx context.Context, value any, opts ...any) error { return nil })},
},
callback: func(c Callback) {
c.Require("alpha", 1)
c.Optional("beta", "ignored")
},
wantErr: true,
},
{
name: "callback with opts",
rules: []Rule{
{Name: "inRange", Handle: HandlerFunc(func(ctx context.Context, value any, opts ...any) error {
if len(opts) != 2 {
return fmt.Errorf("expected 2 options, got %d", len(opts))
}
intVal, ok := value.(int)
if !ok {
return fmt.Errorf("expected int, got %T", value)
}
minVal, ok := opts[0].(int)
if !ok {
return fmt.Errorf("expected int, got %T", value)
}
maxVal, ok := opts[1].(int)
if !ok {
return fmt.Errorf("expected int, got %T", value)
}
if intVal < minVal || intVal > maxVal {
return fmt.Errorf("not in range")
}
return nil
})},
},
callback: func(c Callback) {
c.Require("inRange", 18, 1, 19)
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
v := New()
if err := v.Register(tt.rules...); err != nil {
t.Fatalf("Register() error = %v", err)
}
err := v.Validate(ctx, tt.callback)
if (err != nil) != tt.wantErr {
t.Fatalf("Validate() error = %v, wantErr %v", err, tt.wantErr)
}
if tt.wantErr && tt.errString != "" && err.Error() != tt.errString {
t.Errorf("expected error %q, got %q", tt.errString, err.Error())
}
})
}
}