1
|
|
|
package validation |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"context" |
5
|
|
|
"fmt" |
6
|
|
|
|
7
|
|
|
languagepkg "github.com/muonsoft/language" |
8
|
|
|
"golang.org/x/text/language" |
9
|
|
|
) |
10
|
|
|
|
11
|
|
|
// Scope holds the current state of validation. On the client-side of the package, |
12
|
|
|
// it can be used to build violations. |
13
|
|
|
type Scope struct { |
14
|
|
|
context context.Context |
15
|
|
|
propertyPath PropertyPath |
16
|
|
|
language language.Tag |
17
|
|
|
translator *Translator |
18
|
|
|
violationFactory ViolationFactory |
19
|
|
|
constraints map[string]Constraint |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
// Context returns context value that was passed to the validator by Context argument or |
23
|
|
|
// by creating scoped validator with the validator.WithContext method. |
24
|
|
|
func (s Scope) Context() context.Context { |
25
|
1 |
|
return s.context |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
// BuildViolation is used to create violations in validation methods of constraints. |
29
|
|
|
// This method automatically injects the property path and language of the current validation scope. |
30
|
|
|
func (s Scope) BuildViolation(code, message string) *ViolationBuilder { |
31
|
1 |
|
b := NewViolationBuilder(s.violationFactory).BuildViolation(code, message) |
32
|
1 |
|
b.SetPropertyPath(s.propertyPath) |
33
|
|
|
|
34
|
1 |
|
if s.language != language.Und { |
35
|
1 |
|
b.SetLanguage(s.language) |
36
|
1 |
|
} else if s.context != nil { |
37
|
1 |
|
b.SetLanguage(languagepkg.FromContext(s.context)) |
38
|
|
|
} |
39
|
|
|
|
40
|
1 |
|
return b |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
func (s *Scope) applyOptions(options ...Option) error { |
44
|
1 |
|
for _, option := range options { |
45
|
1 |
|
var err error |
46
|
1 |
|
if o, ok := option.(internalOption); ok { |
47
|
1 |
|
err = o.setUpOnScope(s) |
48
|
|
|
} else { |
49
|
1 |
|
err = option.SetUp() |
50
|
|
|
} |
51
|
1 |
|
if err != nil { |
52
|
1 |
|
return s.describeOptionError(option, err) |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
1 |
|
return nil |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
func (s *Scope) describeOptionError(option Option, err error) error { |
60
|
1 |
|
c, ok := option.(Constraint) |
61
|
1 |
|
if !ok { |
62
|
|
|
return fmt.Errorf(`failed to set up option: %w`, err) |
63
|
|
|
} |
64
|
|
|
|
65
|
1 |
|
if len(s.propertyPath) == 0 { |
66
|
1 |
|
err = fmt.Errorf(`failed to set up constraint "%s": %w`, c.Name(), err) |
67
|
|
|
} else { |
68
|
1 |
|
err = fmt.Errorf(`failed to set up constraint "%s" at path "%s": %w`, c.Name(), s.propertyPath.String(), err) |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
return err |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
func (s Scope) withContext(ctx context.Context) Scope { |
75
|
1 |
|
s.context = ctx |
76
|
|
|
|
77
|
1 |
|
return s |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
func (s Scope) withLanguage(tag language.Tag) Scope { |
81
|
1 |
|
s.language = tag |
82
|
|
|
|
83
|
1 |
|
return s |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
func (s Scope) atProperty(name string) Scope { |
87
|
1 |
|
propertyPath := make(PropertyPath, len(s.propertyPath)+1) |
88
|
1 |
|
copy(propertyPath, s.propertyPath) |
89
|
1 |
|
propertyPath[len(s.propertyPath)] = PropertyNameElement(name) |
90
|
|
|
|
91
|
1 |
|
s.propertyPath = propertyPath |
92
|
|
|
|
93
|
1 |
|
return s |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
func (s Scope) atIndex(index int) Scope { |
97
|
1 |
|
propertyPath := make(PropertyPath, len(s.propertyPath)+1) |
98
|
1 |
|
copy(propertyPath, s.propertyPath) |
99
|
1 |
|
propertyPath[len(s.propertyPath)] = ArrayIndexElement(index) |
100
|
|
|
|
101
|
1 |
|
s.propertyPath = propertyPath |
102
|
|
|
|
103
|
1 |
|
return s |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
func newScope() Scope { |
107
|
1 |
|
translator := newTranslator() |
108
|
|
|
|
109
|
1 |
|
return Scope{ |
110
|
|
|
context: context.Background(), |
111
|
|
|
translator: translator, |
112
|
|
|
violationFactory: newViolationFactory(translator), |
113
|
|
|
constraints: make(map[string]Constraint), |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|