-
Notifications
You must be signed in to change notification settings - Fork 1
/
example_validate_it_test.go
55 lines (48 loc) · 1.32 KB
/
example_validate_it_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
package validation_test
import (
"context"
"fmt"
"github.com/muonsoft/validation"
"github.com/muonsoft/validation/it"
"github.com/muonsoft/validation/validator"
)
type Person struct {
Name string
Surname string
Age int
}
func (p Person) Validate(ctx context.Context, validator *validation.Validator) error {
return validator.Validate(ctx,
validation.StringProperty("name", p.Name, it.IsNotBlank(), it.HasMaxLength(50)),
validation.StringProperty("surname", p.Surname, it.IsNotBlank(), it.HasMaxLength(100)),
validation.NumberProperty[int]("age", p.Age, it.IsBetween(18, 100)),
)
}
func ExampleValidator_ValidateIt() {
persons := []Person{
{
Name: "John",
Surname: "Doe",
Age: 23,
},
{
Name: "",
Surname: "",
Age: 0,
},
}
for i, person := range persons {
err := validator.ValidateIt(context.Background(), person)
if violations, ok := validation.UnwrapViolationList(err); ok {
fmt.Println("person", i, "is not valid:")
for violation := violations.First(); violation != nil; violation = violation.Next() {
fmt.Println(violation)
}
}
}
// Output:
// person 1 is not valid:
// violation at "name": "This value should not be blank."
// violation at "surname": "This value should not be blank."
// violation at "age": "This value should be between 18 and 100."
}