-
Notifications
You must be signed in to change notification settings - Fork 1
/
example_validation_groups_test.go
70 lines (62 loc) · 1.87 KB
/
example_validation_groups_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
package validation_test
import (
"context"
"fmt"
"github.com/muonsoft/validation"
"github.com/muonsoft/validation/it"
"github.com/muonsoft/validation/validator"
)
type User struct {
Email string
Password string
City string
}
func (u User) Validate(ctx context.Context, validator *validation.Validator) error {
return validator.Validate(
ctx,
validation.StringProperty(
"email",
u.Email,
it.IsNotBlank().WhenGroups("registration"),
it.IsEmail().WhenGroups("registration"),
),
validation.StringProperty(
"password",
u.Password,
it.IsNotBlank().WhenGroups("registration"),
it.HasMinLength(7).WhenGroups("registration"),
),
validation.StringProperty(
"city",
u.City,
it.HasMinLength(2), // this constraint belongs to the default group
),
)
}
func ExampleValidator_WithGroups() {
user := User{
Email: "invalid email",
Password: "1234",
City: "Z",
}
err1 := validator.WithGroups("registration").Validate(context.Background(), validation.Valid(user))
err2 := validator.Validate(context.Background(), validation.Valid(user))
if violations, ok := validation.UnwrapViolationList(err1); ok {
fmt.Println("violations for registration group:")
for violation := violations.First(); violation != nil; violation = violation.Next() {
fmt.Println(violation)
}
}
if violations, ok := validation.UnwrapViolationList(err2); ok {
fmt.Println("violations for default group:")
for violation := violations.First(); violation != nil; violation = violation.Next() {
fmt.Println(violation)
}
}
// Output:
// violations for registration group:
// violation at "email": "This value is not a valid email address."
// violation at "password": "This value is too short. It should have 7 characters or more."
// violations for default group:
// violation at "city": "This value is too short. It should have 2 characters or more."
}