Repository navigation
Expand file tree
/
Copy pathvalidator_test.go
More file actions
86 lines (72 loc) · 1.64 KB
/
Copy pathvalidator_test.go
File metadata and controls
86 lines (72 loc) · 1.64 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
package structvalidator
import "testing"
func TestValidateFailsForZeroFields(t *testing.T) {
type ZeroStruct struct {
A int `required:"true"`
}
valid, errors := Validate(ZeroStruct{})
if errors != nil && len(errors) != 0 && valid {
t.Log(errors)
t.Fail()
}
}
func TestValidateMatchesRegex(t *testing.T) {
type StructWithRegex struct {
A string `required:"true" match:"^[a-z0-9]*$"`
}
valid, errors := Validate(StructWithRegex{"a2bcd"})
if !valid {
t.Log(errors)
t.Fail()
}
}
func TestValidateOnlyRequiredFields(t *testing.T) {
type Struct struct {
A int
B string `required:"true"`
}
valid, errors := Validate(Struct{B: "abcd"})
if !valid {
t.Log(errors)
t.Fail()
}
}
func TestValidatesSliceFields(t *testing.T) {
type StructWithSliceFields struct {
A []int `required:"true"`
}
valid, errors := Validate(StructWithSliceFields{})
if valid {
t.Log(errors)
t.Fail()
}
}
func TestValidateShouldIgnoreSpecifiedFields(t *testing.T) {
type StructWithMandatoryFields struct {
A int `required:"true"`
B string `required:"true"`
C string `required:"true"`
D []string `required:"true"`
}
valid, errs := Validate(StructWithMandatoryFields{A: 123}, "B", "C", "D")
if !valid {
t.Log(errs)
t.Fail()
}
}
func TestValidateShouldIgnoreSpecifiedFieldsInEmbeddedStruct(t *testing.T) {
type AnotherStruct struct {
A int `required:"true"`
}
type StructWithMandatoryFields struct {
AnotherStruct
B string `required:"true"`
C string `required:"true"`
D []string `required:"true"`
}
valid, errs := Validate(StructWithMandatoryFields{}, "A", "B", "C", "D")
if !valid {
t.Log(errs)
t.Fail()
}
}