1
|
|
|
package validation_test |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"context" |
5
|
|
|
"fmt" |
6
|
|
|
|
7
|
|
|
"github.com/muonsoft/validation" |
8
|
|
|
"github.com/muonsoft/validation/it" |
9
|
|
|
"github.com/muonsoft/validation/validator" |
10
|
|
|
) |
11
|
|
|
|
12
|
|
|
type Brand struct { |
13
|
|
|
Name string |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
type BrandRepository struct { |
17
|
|
|
brands []Brand |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
func (repository *BrandRepository) FindByName(ctx context.Context, name string) ([]Brand, error) { |
21
|
|
|
found := make([]Brand, 0) |
22
|
|
|
|
23
|
|
|
for _, brand := range repository.brands { |
24
|
|
|
if brand.Name == name { |
25
|
|
|
found = append(found, brand) |
26
|
|
|
} |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
return found, nil |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
// You can declare you own constraint interface to create custom constraints. |
33
|
|
|
type BrandConstraint interface { |
34
|
|
|
validation.Constraint |
35
|
|
|
ValidateBrand(brand *Brand, scope validation.Scope) error |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
// To create your own functional argument for validation simply create a function with |
39
|
|
|
// a typed value and use the validation.NewArgument constructor. |
40
|
|
|
func BrandArgument(brand *Brand, options ...validation.Option) validation.Argument { |
41
|
|
|
return validation.NewArgument(options, func(constraint validation.Constraint, scope validation.Scope) error { |
42
|
|
|
if c, ok := constraint.(BrandConstraint); ok { |
43
|
|
|
return c.ValidateBrand(brand, scope) |
44
|
|
|
} |
45
|
|
|
// If you want to use built-in constraints for checking for nil or empty values |
46
|
|
|
// such as it.IsNil() or it.IsBlank(). |
47
|
|
|
if c, ok := constraint.(validation.NilConstraint); ok { |
48
|
|
|
if brand == nil { |
49
|
|
|
return c.ValidateNil(scope) |
50
|
|
|
} |
51
|
|
|
return nil |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return validation.NewInapplicableConstraintError(constraint, "Brand") |
55
|
|
|
}) |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
// UniqueBrandConstraint implements BrandConstraint. |
59
|
|
|
type UniqueBrandConstraint struct { |
60
|
|
|
brands *BrandRepository |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
func (c *UniqueBrandConstraint) SetUp() error { |
64
|
|
|
return nil |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
func (c *UniqueBrandConstraint) Name() string { |
68
|
|
|
return "UniqueBrandConstraint" |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
func (c *UniqueBrandConstraint) ValidateBrand(brand *Brand, scope validation.Scope) error { |
72
|
|
|
// usually, you should ignore empty values |
73
|
|
|
// to check for an empty value you should use it.NotBlankConstraint |
74
|
|
|
if brand == nil { |
75
|
|
|
return nil |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
// you can pass the context value from the scope |
79
|
|
|
brands, err := c.brands.FindByName(scope.Context(), brand.Name) |
80
|
|
|
// here you can return a service error so that the validation process |
81
|
|
|
// is stopped immediately |
82
|
|
|
if err != nil { |
83
|
|
|
return err |
84
|
|
|
} |
85
|
|
|
if len(brands) == 0 { |
86
|
|
|
return nil |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
// use the scope to build violation with translations |
90
|
|
|
return scope. |
91
|
|
|
BuildViolation("notUniqueBrand", `Brand with name "{{ name }}" already exists.`). |
92
|
|
|
// you can inject parameter value to the message here |
93
|
|
|
AddParameter("{{ name }}", brand.Name). |
94
|
|
|
CreateViolation() |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
func ExampleNewArgument_customArgumentConstraintValidator() { |
98
|
|
|
repository := &BrandRepository{brands: []Brand{{"Apple"}, {"Orange"}}} |
99
|
|
|
isEntityUnique := &UniqueBrandConstraint{brands: repository} |
100
|
|
|
|
101
|
|
|
brand := Brand{Name: "Apple"} |
102
|
|
|
ctx := context.WithValue(context.Background(), exampleKey, "value") |
103
|
|
|
|
104
|
|
|
err := validator.Validate( |
105
|
|
|
// you can pass here the context value to the validation scope |
106
|
|
|
validation.Context(ctx), |
107
|
|
|
BrandArgument(&brand, it.IsNotBlank(), isEntityUnique), |
108
|
|
|
) |
109
|
|
|
|
110
|
|
|
violations := err.(validation.ViolationList) |
111
|
|
|
for _, violation := range violations { |
112
|
|
|
fmt.Println(violation.Error()) |
113
|
|
|
} |
114
|
|
|
// Output: |
115
|
|
|
// violation: Brand with name "Apple" already exists. |
116
|
|
|
} |
117
|
|
|
|