forked from koding/multiconfig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validator_test.go
50 lines (40 loc) · 962 Bytes
/
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
package multiconfig
import "testing"
func TestValidators(t *testing.T) {
s := getDefaultServer()
s.Name = ""
err := (&RequiredValidator{}).Validate(s)
if err == nil {
t.Fatal("Name should be required")
}
}
func TestValidatorsEmbededStruct(t *testing.T) {
s := getDefaultServer()
s.Postgres.Port = 0
err := (&RequiredValidator{}).Validate(s)
if err == nil {
t.Fatal("Port should be required")
}
}
func TestValidatorsCustomTag(t *testing.T) {
s := getDefaultServer()
validator := (&RequiredValidator{
TagName: "customRequired",
TagValue: "yes",
})
// test happy path
err := validator.Validate(s)
if err != nil {
t.Fatal(err)
}
// validate sad case
s.Postgres.Port = 0
err = validator.Validate(s)
if err == nil {
t.Fatal("Port should be required")
}
errStr := "multiconfig: field 'Postgres.Port' is required"
if err.Error() != errStr {
t.Fatalf("Err string is wrong: expected %s, got: %s", errStr, err.Error())
}
}