-
Notifications
You must be signed in to change notification settings - Fork 6
/
validator_test.go
55 lines (48 loc) · 1.06 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
package gody
import (
"testing"
"github.com/guiferpa/gody/v2/rule/ruletest"
)
func TestValidator(t *testing.T) {
payload := struct {
A int `validate:"test"`
}{10}
validator := NewValidator()
rule := ruletest.NewRule("test", true, nil)
if err := validator.AddRules(rule); err != nil {
t.Error("Unexpected error")
return
}
validated, err := validator.Validate(payload)
if !validated {
t.Error("Validated result is not expected")
return
}
if err != nil {
t.Error("Error result from validate is not expected")
return
}
if !rule.ValidateCalled {
t.Error("The rule validate wasn't call")
return
}
}
func TestDuplicatedRule(t *testing.T) {
validator := NewValidator()
rule := ruletest.NewRule("a", true, nil)
rules := []Rule{
rule,
ruletest.NewRule("b", true, nil),
ruletest.NewRule("c", true, nil),
rule,
}
err := validator.AddRules(rules...)
if err == nil {
t.Error("Unexpected nil value for duplicated rule error")
return
}
if _, ok := err.(*ErrDuplicatedRule); !ok {
t.Errorf("Unexpected error type: got: %v", err)
return
}
}