-
Notifications
You must be signed in to change notification settings - Fork 3
/
validator_test.go
91 lines (75 loc) · 1.71 KB
/
validator_test.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
package validator
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
)
func TestValidateValue_Int_Successfully(t *testing.T) {
ctx := context.Background()
rules := []Rule{
NewRequired(),
NewNumber(1, 3),
}
err := ValidateValue(ctx, 1, rules...)
assert.NoError(t, err)
}
func TestValidateValue_Int_Failure(t *testing.T) {
ctx := context.Background()
rules := []Rule{
NewRequired(),
NewNumber(1, 3),
}
err := ValidateValue(ctx, 0, rules...)
assert.Error(t, err)
assert.Equal(t, "Value must be no less than 1.", err.Error())
assert.ErrorAs(t, err, &Result{})
expectedResult := NewResult().
WithError(
NewValidationError("Value must be no less than 1.").
WithParams(map[string]any{"min": int64(1), "max": int64(3)}),
)
assert.Equal(t, expectedResult, err)
}
func TestValidateValue_IntNilPtrValue_Failure(t *testing.T) {
ctx := context.Background()
rules := []Rule{
NewNumber(1, 3),
}
err := ValidateValue(ctx, nil, rules...)
assert.Error(t, err)
assert.Equal(t, "Value must be a number.", err.Error())
}
func TestValidateValue_IntPtrValue_Successfully(t *testing.T) {
ctx := context.Background()
rules := []Rule{
NewNumber(1, 3),
}
v := 2
err := ValidateValue(ctx, &v, rules...)
assert.NoError(t, err)
}
func TestValidate_Map_Successfully(t *testing.T) {
ctx := context.Background()
rules := RuleSet{
"count": {
NewRequired(),
NewNumber(1, 3),
},
}
data := map[string]any{
"count": 1,
}
err := Validate(ctx, data, rules)
assert.NoError(t, err)
}
func TestValidate_Nil_Failure(t *testing.T) {
ctx := context.Background()
rules := RuleSet{
"count": {
NewRequired(),
NewNumber(1, 3),
},
}
err := Validate(ctx, nil, rules)
assert.Error(t, err)
}