-
Notifications
You must be signed in to change notification settings - Fork 0
/
fender_example_test.go
92 lines (83 loc) · 2.05 KB
/
fender_example_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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package fender_test
import (
"context"
"fmt"
"github.com/foomo/fender"
"github.com/foomo/fender/fend"
"github.com/foomo/fender/rule"
)
func ExampleAll() {
err := fender.All(
context.Background(),
fend.Field("one", "", rule.Required[string], rule.StringMin(10)),
fend.Field("two", "", rule.Required[string], rule.StringMin(10)),
)
// check for fender error
if fendErr := fender.AsError(err); fendErr != nil {
fmt.Println(err)
} else if err != nil {
panic(err)
}
// Output: one:required:min=10;two:required:min=10
}
func ExampleFirst() {
err := fender.First(
context.Background(),
fend.Field("one", "", rule.Required[string], rule.StringMin(10)),
fend.Field("two", "", rule.Required[string], rule.StringMin(10)),
)
// check for fender error
if fendErr := fender.AsError(err); fendErr != nil {
fmt.Println(err)
} else if err != nil {
panic(err)
}
// Output: one:required
}
func ExampleAllFirst() {
err := fender.AllFirst(
context.Background(),
fend.Field("one", "", rule.Required[string], rule.StringMin(10)),
fend.Field("two", "", rule.Required[string], rule.StringMin(10)),
)
// check for fender error
if fendErr := fender.AsError(err); fendErr != nil {
fmt.Println(err)
} else if err != nil {
panic(err)
}
// Output: one:required:min=10
}
func ExampleErrors() { //nolint:govet
err := fender.All(
context.Background(),
fend.Field("one", "", rule.Required[string], rule.StringMin(10)),
fend.Field("two", "", rule.Required[string], rule.StringMin(10)),
)
// cast fender error
if fenderErr := fender.AsError(err); fenderErr != nil {
// iterate fend errors
for _, err := range fenderErr.Errors() {
// cast fend error
if fendErr := fend.AsError(err); fendErr != nil {
fmt.Println(fendErr.Name())
// iterate rule errors
for _, err := range fendErr.Errors() {
// cast rule error
if ruleErr := rule.AsError(err); ruleErr != nil {
fmt.Println(ruleErr.Error())
}
}
}
}
} else if err != nil {
panic(err)
}
// Output:
// one
// required
// min=10
// two
// required
// min=10
}