-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample_check_no_violations_test.go
83 lines (72 loc) · 2.12 KB
/
example_check_no_violations_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
package validation_test
import (
"context"
"fmt"
"github.com/muonsoft/validation"
"github.com/muonsoft/validation/it"
"github.com/muonsoft/validation/validator"
)
type Vehicle struct {
Model string
MaxSpeed int
}
func (v Vehicle) Validate(ctx context.Context, validator *validation.Validator) error {
return validator.Validate(ctx,
validation.StringProperty("model", v.Model, it.IsNotBlank(), it.HasMaxLength(100)),
validation.NumberProperty[int]("maxSpeed", v.MaxSpeed, it.IsBetween(50, 200)),
)
}
type Car struct {
Vehicle
PassengerSeats int
}
func (c Car) Validate(ctx context.Context, validator *validation.Validator) error {
return validator.Validate(ctx,
validation.CheckNoViolations(c.Vehicle.Validate(ctx, validator)),
validation.NumberProperty[int]("passengerSeats", c.PassengerSeats, it.IsBetween(2, 6)),
)
}
type Truck struct {
Vehicle
LoadCapacity float64
}
func (t Truck) Validate(ctx context.Context, validator *validation.Validator) error {
return validator.Validate(ctx,
validation.CheckNoViolations(t.Vehicle.Validate(ctx, validator)),
validation.NumberProperty[float64]("loadCapacity", t.LoadCapacity, it.IsBetween(10.0, 200.0)),
)
}
func ExampleCheckNoViolations() {
vehicles := []validation.Validatable{
Car{
Vehicle: Vehicle{
Model: "Audi",
MaxSpeed: 10,
},
PassengerSeats: 1,
},
Truck{
Vehicle: Vehicle{
Model: "Benz",
MaxSpeed: 20,
},
LoadCapacity: 5,
},
}
for i, vehicle := range vehicles {
err := validator.ValidateIt(context.Background(), vehicle)
if violations, ok := validation.UnwrapViolationList(err); ok {
fmt.Println("vehicle", i, "is not valid:")
for violation := violations.First(); violation != nil; violation = violation.Next() {
fmt.Println(violation)
}
}
}
// Output:
// vehicle 0 is not valid:
// violation at "maxSpeed": "This value should be between 50 and 200."
// violation at "passengerSeats": "This value should be between 2 and 6."
// vehicle 1 is not valid:
// violation at "maxSpeed": "This value should be between 50 and 200."
// violation at "loadCapacity": "This value should be between 10 and 200."
}