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/validationtest" |
10
|
|
|
"github.com/muonsoft/validation/validator" |
11
|
|
|
"github.com/stretchr/testify/assert" |
12
|
|
|
) |
13
|
|
|
|
14
|
|
|
func TestValidateEach_WhenSliceOfStrings_ExpectViolationOnEachElement(t *testing.T) { |
15
|
|
|
strings := []string{"", ""} |
16
|
|
|
|
17
|
|
|
err := validator.ValidateEach(strings, it.IsNotBlank()) |
18
|
|
|
|
19
|
|
|
validationtest.AssertIsViolationList(t, err, func(t *testing.T, violations validation.ViolationList) bool { |
20
|
|
|
t.Helper() |
21
|
|
|
if assert.Len(t, violations, 2) { |
22
|
|
|
assert.Equal(t, code.NotBlank, violations[0].Code()) |
23
|
|
|
assert.Equal(t, "[0]", violations[0].PropertyPath().String()) |
24
|
|
|
assert.Equal(t, code.NotBlank, violations[1].Code()) |
25
|
|
|
assert.Equal(t, "[1]", violations[1].PropertyPath().String()) |
26
|
|
|
} |
27
|
|
|
return true |
28
|
|
|
}) |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
func TestValidateEach_WhenMapOfStrings_ExpectViolationOnEachElement(t *testing.T) { |
32
|
|
|
strings := map[string]string{"key1": "", "key2": ""} |
33
|
|
|
|
34
|
|
|
err := validator.ValidateEach(strings, it.IsNotBlank()) |
35
|
|
|
|
36
|
|
|
validationtest.AssertIsViolationList(t, err, func(t *testing.T, violations validation.ViolationList) bool { |
37
|
|
|
t.Helper() |
38
|
|
|
if assert.Len(t, violations, 2) { |
39
|
|
|
assert.Equal(t, code.NotBlank, violations[0].Code()) |
40
|
|
|
assert.Contains(t, violations[0].PropertyPath().String(), "key") |
41
|
|
|
assert.Equal(t, code.NotBlank, violations[1].Code()) |
42
|
|
|
assert.Contains(t, violations[1].PropertyPath().String(), "key") |
43
|
|
|
} |
44
|
|
|
return true |
45
|
|
|
}) |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
func TestValidateEachString_WhenSliceOfStrings_ExpectViolationOnEachElement(t *testing.T) { |
49
|
|
|
strings := []string{"", ""} |
50
|
|
|
|
51
|
|
|
err := validator.ValidateEachString(strings, it.IsNotBlank()) |
52
|
|
|
|
53
|
|
|
validationtest.AssertIsViolationList(t, err, func(t *testing.T, violations validation.ViolationList) bool { |
54
|
|
|
t.Helper() |
55
|
|
|
if assert.Len(t, violations, 2) { |
56
|
|
|
assert.Equal(t, code.NotBlank, violations[0].Code()) |
57
|
|
|
assert.Equal(t, "[0]", violations[0].PropertyPath().String()) |
58
|
|
|
assert.Equal(t, code.NotBlank, violations[1].Code()) |
59
|
|
|
assert.Equal(t, "[1]", violations[1].PropertyPath().String()) |
60
|
|
|
} |
61
|
|
|
return true |
62
|
|
|
}) |
63
|
|
|
} |
64
|
|
|
|