-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslice_test.go
More file actions
61 lines (48 loc) · 1.33 KB
/
slice_test.go
File metadata and controls
61 lines (48 loc) · 1.33 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
package validate_test
import (
"testing"
"github.com/SLASH2NL/validate"
"github.com/stretchr/testify/require"
)
type testSlice struct {
Name string
Amount int
}
func TestSlice(t *testing.T) {
data := []testSlice{
{Name: "John Deer", Amount: 9},
{Name: "Deer John", Amount: 1},
}
err := validate.Slice("data", data).Items("total", func(value testSlice) error {
if value.Amount < 5 {
return &validate.Violation{Code: "max"}
}
return nil
})
require.Error(t, err)
errs := validate.Collect(err)
require.Equal(t, 1, len(errs))
require.Equal(t, "data.*.total", errs[0].Path)
require.Equal(t, "data.1.total", errs[0].ExactPath)
require.Equal(t, "max", errs[0].Violations[0].Code)
}
func TestSliceResolve(t *testing.T) {
data := []testSlice{
{Name: "John Deer", Amount: 9},
{Name: "Deer John", Amount: 1},
}
resolver := func(t testSlice) int { return t.Amount }
validator := func(value int) error {
if value < 5 {
return &validate.Violation{Code: "max"}
}
return nil
}
err := validate.Slice("data", data).Items("amount", validate.Resolve(resolver, validator)...)
require.Error(t, err)
errs := validate.Collect(err)
require.Equal(t, 1, len(errs))
require.Equal(t, "data.*.amount", errs[0].Path)
require.Equal(t, "data.1.amount", errs[0].ExactPath)
require.Equal(t, "max", errs[0].Violations[0].Code)
}