-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample_validatable_struct_test.go
77 lines (68 loc) · 2.06 KB
/
example_validatable_struct_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
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
package validation_test
import (
"context"
"fmt"
"github.com/muonsoft/validation"
"github.com/muonsoft/validation/it"
"github.com/muonsoft/validation/validator"
)
type Product struct {
Name string
Tags []string
Components []Component
}
func (p Product) Validate(ctx context.Context, validator *validation.Validator) error {
return validator.Validate(
ctx,
validation.StringProperty("name", p.Name, it.IsNotBlank()),
validation.AtProperty(
"tags",
validation.Countable(len(p.Tags), it.HasMinCount(5)),
validation.Comparables[string](p.Tags, it.HasUniqueValues[string]()),
validation.EachString(p.Tags, it.IsNotBlank()),
),
validation.AtProperty(
"components",
validation.Countable(len(p.Components), it.HasMinCount(1)),
// this runs validation on each of the components
validation.ValidSlice(p.Components),
),
)
}
type Component struct {
ID int
Name string
Tags []string
}
func (c Component) Validate(ctx context.Context, validator *validation.Validator) error {
return validator.Validate(
ctx,
validation.StringProperty("name", c.Name, it.IsNotBlank()),
validation.CountableProperty("tags", len(c.Tags), it.HasMinCount(1)),
)
}
func ExampleValid_validatableStruct() {
p := Product{
Name: "",
Tags: []string{"device", "", "phone", "device"},
Components: []Component{
{
ID: 1,
Name: "",
},
},
}
err := validator.Validate(context.Background(), validation.Valid(p))
if violations, ok := validation.UnwrapViolationList(err); ok {
for violation := violations.First(); violation != nil; violation = violation.Next() {
fmt.Println(violation)
}
}
// Output:
// violation at "name": "This value should not be blank."
// violation at "tags": "This collection should contain 5 elements or more."
// violation at "tags": "This collection should contain only unique elements."
// violation at "tags[1]": "This value should not be blank."
// violation at "components[0].name": "This value should not be blank."
// violation at "components[0].tags": "This collection should contain 1 element or more."
}