Code

< 40 %
40-60 %
> 60 %
1
package validation
2
3
import "strings"
4
5
// TemplateParameter is injected into the message while rendering the template.
6
type TemplateParameter struct {
7
	// Key is the marker in the string that will be replaced by value.
8
	// In general, it is recommended to use double curly braces around the key name.
9
	// Example: {{ keyName }}
10
	Key string
11
12
	// Value is set by constraint when building violation.
13
	Value string
14
15
	// NeedsTranslation marks that the template value needs to be translated.
16
	NeedsTranslation bool
17
}
18
19
// TemplateParameterList is a list of template parameters that can be injection into violation message.
20
type TemplateParameterList []TemplateParameter
21
22
// Prepend returns [TemplateParameterList] prepended by given parameters.
23
func (params TemplateParameterList) Prepend(parameters ...TemplateParameter) TemplateParameterList {
24 1
	return append(parameters, params...)
25
}
26
27
func renderMessage(template string, parameters []TemplateParameter) string {
28 1
	message := template
29
30 1
	for _, p := range parameters {
31 1
		message = strings.ReplaceAll(message, p.Key, p.Value)
32
	}
33
34 1
	return message
35
}
36