1
|
|
|
package it |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"strings" |
5
|
|
|
|
6
|
|
|
"github.com/muonsoft/validation" |
7
|
|
|
"github.com/muonsoft/validation/code" |
8
|
|
|
"github.com/muonsoft/validation/message" |
9
|
|
|
) |
10
|
|
|
|
11
|
|
|
// ChoiceConstraint is used to ensure that the given value corresponds to one of the expected choices. |
12
|
|
|
type ChoiceConstraint struct { |
13
|
|
|
choices map[string]bool |
14
|
|
|
choicesValue string |
15
|
|
|
code string |
16
|
|
|
messageTemplate string |
17
|
|
|
messageParameters validation.TemplateParameterList |
18
|
|
|
isIgnored bool |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
// IsOneOfStrings creates a ChoiceConstraint for checking that values are in the expected list of strings. |
22
|
|
|
func IsOneOfStrings(values ...string) ChoiceConstraint { |
23
|
1 |
|
choices := make(map[string]bool, len(values)) |
24
|
1 |
|
for _, value := range values { |
25
|
1 |
|
choices[value] = true |
26
|
|
|
} |
27
|
|
|
|
28
|
1 |
|
return ChoiceConstraint{ |
29
|
|
|
choices: choices, |
30
|
|
|
choicesValue: strings.Join(values, ", "), |
31
|
|
|
code: code.NoSuchChoice, |
32
|
|
|
messageTemplate: message.NoSuchChoice, |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
// SetUp will return an error if the list of choices is empty. |
37
|
|
|
func (c ChoiceConstraint) SetUp() error { |
38
|
1 |
|
if len(c.choices) == 0 { |
39
|
1 |
|
return errEmptyChoices |
40
|
|
|
} |
41
|
|
|
|
42
|
1 |
|
return nil |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
// Name is the constraint name. |
46
|
|
|
func (c ChoiceConstraint) Name() string { |
47
|
1 |
|
return "ChoiceConstraint" |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
// Code overrides default code for produced violation. |
51
|
|
|
func (c ChoiceConstraint) Code(code string) ChoiceConstraint { |
52
|
1 |
|
c.code = code |
53
|
1 |
|
return c |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
// Message sets the violation message template. You can set custom template parameters |
57
|
|
|
// for injecting its values into the final message. Also, you can use default parameters: |
58
|
|
|
// |
59
|
|
|
// {{ choices }} - a comma-separated list of available choices; |
60
|
|
|
// {{ value }} - the current (invalid) value. |
61
|
|
|
func (c ChoiceConstraint) Message(template string, parameters ...validation.TemplateParameter) ChoiceConstraint { |
62
|
1 |
|
c.messageTemplate = template |
63
|
1 |
|
c.messageParameters = parameters |
64
|
1 |
|
return c |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// When enables conditional validation of this constraint. If the expression evaluates to false, |
68
|
|
|
// then the constraint will be ignored. |
69
|
|
|
func (c ChoiceConstraint) When(condition bool) ChoiceConstraint { |
70
|
1 |
|
c.isIgnored = !condition |
71
|
1 |
|
return c |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
func (c ChoiceConstraint) ValidateString(value *string, scope validation.Scope) error { |
75
|
1 |
|
if c.isIgnored || value == nil || *value == "" { |
76
|
1 |
|
return nil |
77
|
|
|
} |
78
|
1 |
|
if c.choices[*value] { |
79
|
1 |
|
return nil |
80
|
|
|
} |
81
|
|
|
|
82
|
1 |
|
return scope. |
83
|
|
|
BuildViolation(c.code, c.messageTemplate). |
84
|
|
|
SetParameters( |
85
|
|
|
c.messageParameters.Prepend( |
86
|
|
|
validation.TemplateParameter{Key: "{{ value }}", Value: *value}, |
87
|
|
|
validation.TemplateParameter{Key: "{{ choices }}", Value: c.choicesValue}, |
88
|
|
|
)..., |
89
|
|
|
). |
90
|
|
|
CreateViolation() |
91
|
|
|
} |
92
|
|
|
|