forked from cohesivestack/valgo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidator_context_test.go
More file actions
94 lines (71 loc) · 2.8 KB
/
Copy pathvalidator_context_test.go
File metadata and controls
94 lines (71 loc) · 2.8 KB
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
84
85
86
87
88
89
90
91
92
93
94
package valgo
import (
"testing"
"github.com/stretchr/testify/assert"
)
type validatorContextLocaleFallbackValidator struct {
context *ValidatorContext
}
func newValidatorContextLocaleFallbackValidator(locales ...*Locale) *validatorContextLocaleFallbackValidator {
context := NewContext("value", "field", "Field")
context.WithLocaleFallback(locales...)
return &validatorContextLocaleFallbackValidator{context: context}
}
func (validator *validatorContextLocaleFallbackValidator) Invalid(errorKey string) *validatorContextLocaleFallbackValidator {
validator.context.Add(
func() bool {
return false
},
errorKey,
)
return validator
}
func (validator *validatorContextLocaleFallbackValidator) Context() *ValidatorContext {
return validator.context
}
func TestValidatorContextWithLocaleFallbackUsesFallbackForMissingKey(t *testing.T) {
locale := &Locale{
"custom_error": "{{title}} uses the fallback message",
}
validation := New()
originalLocale := validation._locale
validation.Is(newValidatorContextLocaleFallbackValidator(locale).Invalid("custom_error"))
assert.Contains(t, validation.Errors()["field"].Messages(), "Field uses the fallback message")
assert.NotSame(t, originalLocale, validation._locale)
assert.NotContains(t, *originalLocale, "custom_error")
}
func TestValidatorContextWithLocaleFallbackDoesNotOverrideActiveLocale(t *testing.T) {
activeLocale := &Locale{
"custom_error": "{{title}} uses the active locale message",
}
fallbackLocale := &Locale{
"custom_error": "{{title}} uses the fallback message",
}
validation := New(Options{Locale: activeLocale})
originalLocale := validation._locale
validation.Is(newValidatorContextLocaleFallbackValidator(fallbackLocale).Invalid("custom_error"))
assert.Contains(t, validation.Errors()["field"].Messages(), "Field uses the active locale message")
assert.Same(t, originalLocale, validation._locale)
}
func TestValidatorContextWithLocaleFallbackDoesNotOverrideBuiltInLocale(t *testing.T) {
locale := &Locale{
ErrorKeyBlank: "{{title}} uses the fallback message",
}
validation := New()
originalLocale := validation._locale
validation.Is(newValidatorContextLocaleFallbackValidator(locale).Invalid(ErrorKeyBlank))
assert.Contains(t, validation.Errors()["field"].Messages(), "Field must be blank")
assert.Same(t, originalLocale, validation._locale)
}
func TestValidatorContextWithLocaleFallbackKeepsFirstFallbackEntry(t *testing.T) {
firstLocale := &Locale{
"custom_error": "{{title}} uses the first fallback message",
}
secondLocale := &Locale{
"custom_error": "{{title}} uses the second fallback message",
}
validation := New().Is(
newValidatorContextLocaleFallbackValidator(firstLocale, secondLocale).Invalid("custom_error"),
)
assert.Contains(t, validation.Errors()["field"].Messages(), "Field uses the first fallback message")
}