1
|
|
|
package test |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"testing" |
5
|
|
|
|
6
|
|
|
"github.com/muonsoft/validation" |
7
|
|
|
"github.com/muonsoft/validation/code" |
8
|
|
|
"github.com/muonsoft/validation/it" |
9
|
|
|
"github.com/muonsoft/validation/message" |
10
|
|
|
"github.com/muonsoft/validation/validationtest" |
11
|
|
|
"github.com/muonsoft/validation/validator" |
12
|
|
|
"github.com/stretchr/testify/assert" |
13
|
|
|
) |
14
|
|
|
|
15
|
|
|
type Product struct { |
16
|
|
|
Name string |
17
|
|
|
Tags []string |
18
|
|
|
Components []Component |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
func (p Product) Validate(validator *validation.Validator) error { |
22
|
|
|
return validator.Validate( |
23
|
|
|
validation.String( |
24
|
|
|
&p.Name, |
25
|
|
|
validation.PropertyName("name"), |
26
|
|
|
it.IsNotBlank(), |
27
|
|
|
), |
28
|
|
|
validation.Iterable( |
29
|
|
|
p.Tags, |
30
|
|
|
validation.PropertyName("tags"), |
31
|
|
|
it.HasMinCount(1), |
32
|
|
|
), |
33
|
|
|
validation.Iterable( |
34
|
|
|
p.Components, |
35
|
|
|
validation.PropertyName("components"), |
36
|
|
|
it.HasMinCount(1), |
37
|
|
|
), |
38
|
|
|
) |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
type Component struct { |
42
|
|
|
ID int |
43
|
|
|
Name string |
44
|
|
|
Tags []string |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
func (c Component) Validate(validator *validation.Validator) error { |
48
|
|
|
return validator.Validate( |
49
|
|
|
validation.String( |
50
|
|
|
&c.Name, |
51
|
|
|
validation.PropertyName("name"), |
52
|
|
|
it.IsNotBlank(), |
53
|
|
|
), |
54
|
|
|
validation.Iterable( |
55
|
|
|
c.Tags, |
56
|
|
|
validation.PropertyName("tags"), |
57
|
|
|
it.HasMinCount(1), |
58
|
|
|
), |
59
|
|
|
) |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
func TestValidateValue_WhenStructWithComplexRules_ExpectViolations(t *testing.T) { |
63
|
|
|
p := Product{ |
64
|
|
|
Name: "", |
65
|
|
|
Components: []Component{ |
66
|
|
|
{ |
67
|
|
|
ID: 1, |
68
|
|
|
Name: "", |
69
|
|
|
}, |
70
|
|
|
}, |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
err := validator.ValidateValue(p) |
74
|
|
|
|
75
|
|
|
validationtest.AssertIsViolationList(t, err, func(t *testing.T, violations validation.ViolationList) bool { |
76
|
|
|
t.Helper() |
77
|
|
|
if assert.Len(t, violations, 4) { |
78
|
|
|
assert.Equal(t, code.NotBlank, violations[0].Code()) |
79
|
|
|
assert.Equal(t, "name", violations[0].PropertyPath().String()) |
80
|
|
|
assert.Equal(t, code.CountTooFew, violations[1].Code()) |
81
|
|
|
assert.Equal(t, "tags", violations[1].PropertyPath().String()) |
82
|
|
|
assert.Equal(t, code.NotBlank, violations[2].Code()) |
83
|
|
|
assert.Equal(t, "components[0].name", violations[2].PropertyPath().String()) |
84
|
|
|
assert.Equal(t, code.CountTooFew, violations[3].Code()) |
85
|
|
|
assert.Equal(t, "components[0].tags", violations[3].PropertyPath().String()) |
86
|
|
|
} |
87
|
|
|
return true |
88
|
|
|
}) |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
func TestValidateValue_WhenValidatableString_ExpectValidationExecutedWithPassedOptionsWithoutConstraints(t *testing.T) { |
92
|
|
|
validatable := mockValidatableString{value: ""} |
93
|
|
|
|
94
|
|
|
err := validator.ValidateValue( |
95
|
|
|
validatable, |
96
|
|
|
validation.PropertyName("top"), |
97
|
|
|
it.IsNotBlank().Message("ignored"), |
98
|
|
|
) |
99
|
|
|
|
100
|
|
|
assertHasOneViolation(code.NotBlank, message.NotBlank, "top.value")(t, err) |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
func TestValidateValidatable_WhenValidatableString_ExpectValidationExecutedWithPassedOptionsWithoutConstraints(t *testing.T) { |
104
|
|
|
validatable := mockValidatableString{value: ""} |
105
|
|
|
|
106
|
|
|
err := validator.ValidateValidatable( |
107
|
|
|
validatable, |
108
|
|
|
validation.PropertyName("top"), |
109
|
|
|
it.IsNotBlank().Message("ignored"), |
110
|
|
|
) |
111
|
|
|
|
112
|
|
|
assertHasOneViolation(code.NotBlank, message.NotBlank, "top.value")(t, err) |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
func TestValidateValue_WhenValidatableStruct_ExpectValidationExecutedWithPassedOptionsWithoutConstraints(t *testing.T) { |
116
|
|
|
validatable := mockValidatableStruct{} |
117
|
|
|
|
118
|
|
|
err := validator.ValidateValue( |
119
|
|
|
validatable, |
120
|
|
|
validation.PropertyName("top"), |
121
|
|
|
it.IsNotBlank().Message("ignored"), |
122
|
|
|
) |
123
|
|
|
|
124
|
|
|
validationtest.AssertIsViolationList(t, err, func(t *testing.T, violations validation.ViolationList) bool { |
125
|
|
|
t.Helper() |
126
|
|
|
if assert.Len(t, violations, 4) { |
127
|
|
|
assert.Equal(t, "top.intValue", violations[0].PropertyPath().String()) |
128
|
|
|
assert.Equal(t, "top.floatValue", violations[1].PropertyPath().String()) |
129
|
|
|
assert.Equal(t, "top.stringValue", violations[2].PropertyPath().String()) |
130
|
|
|
assert.Equal(t, "top.structValue.value", violations[3].PropertyPath().String()) |
131
|
|
|
} |
132
|
|
|
return true |
133
|
|
|
}) |
134
|
|
|
} |
135
|
|
|
|