-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlaravalidate_test.go
188 lines (154 loc) · 4.26 KB
/
laravalidate_test.go
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package laravalidate
import (
"context"
"encoding/json"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
type testValidator struct {
*testing.T
}
func (v *testValidator) AssertValid(input any) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
assert.Nilf(v.T, GoValidate(ctx, nil, input), "expected no validation error %T%+v", input, input)
}
func (v *testValidator) AssertInvalid(input any) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
assert.NotNilf(v.T, GoValidate(ctx, nil, input), "expected a validation error %T%+v", input, input)
}
func TestValidate(t *testing.T) {
v := testValidator{t}
v.AssertValid(struct{}{})
type SimpleNameStruct struct {
Name *string `validate:"required"`
}
v.AssertInvalid(SimpleNameStruct{})
name := "John Doe"
v.AssertValid(SimpleNameStruct{
Name: &name,
})
// Test nested structs
type A struct {
A *string `validate:"required"`
}
a := A{&name}
type B struct {
B *A
BB A
}
b := B{
B: &a,
BB: a,
}
type C struct {
C *B
CC B
}
c := C{
C: &b,
CC: b,
}
type D struct {
D *C `validate:"required"`
DD C
}
d := D{
D: &c,
DD: c,
}
v.AssertValid(d)
d.D = nil
v.AssertInvalid(d)
type Empty struct{}
// Test validateInner
type ValidateInner struct {
List []*string `validate:"required" validateInner:"required"`
}
v.AssertInvalid(ValidateInner{})
v.AssertInvalid(ValidateInner{List: []*string{nil}})
v.AssertValid(ValidateInner{List: []*string{&name}})
type ValidateOnlyInner struct {
List []*string `validateInner:"required"`
}
v.AssertValid(ValidateOnlyInner{})
v.AssertInvalid(ValidateOnlyInner{List: []*string{nil}})
v.AssertValid(ValidateOnlyInner{List: []*string{&name}})
type ValidateOnlyOuter struct {
List []*string `validate:"required"`
}
v.AssertInvalid(ValidateOnlyOuter{})
v.AssertValid(ValidateOnlyOuter{List: []*string{nil}})
v.AssertValid(ValidateOnlyOuter{List: []*string{&name}})
// Test nested slices
type ValidateNestedSlice struct {
list [][][]struct{} `validate:"not_nil"`
}
v.AssertInvalid(ValidateNestedSlice{})
v.AssertValid(ValidateNestedSlice{list: [][][]struct{}{}})
v.AssertValid(ValidateNestedSlice{list: [][][]struct{}{{nil}}})
// Nil validation
type NilValidation struct {
Field string `validate:"required"`
}
var nilValidation *NilValidation
v.AssertInvalid(nilValidation)
type NilValidation2 struct {
Foo *any `validate:"required"`
Bar any `validate:"required"`
}
v.AssertInvalid(NilValidation2{})
}
func TestErrorMessages(t *testing.T) {
err := JsonValidate(nil, nil, struct {
Message string `validate:"required" json:"message"`
}{})
assert.NotNil(t, err)
typedErr, ok := err.(*ValidationError)
assert.True(t, ok)
assert.Len(t, typedErr.Errors, 1)
firstErr := typedErr.Errors[0]
assert.Equal(t, "message", firstErr.Path)
assert.Len(t, firstErr.Errors, 1)
firstValidatorErr := firstErr.Errors[0]
assert.Equal(t, FieldValidatorError{
Rule: "required",
Hint: "required",
Message: "The message field is required.",
}, firstValidatorErr)
laravelError := typedErr.ToLaravelError()
laravelErrorBytes, err := json.Marshal(laravelError)
assert.NoError(t, err)
assert.Equal(t, `{"errors":{"message":["The message field is required."]},"message":"Form contains errors"}`, string(laravelErrorBytes))
}
type TestCustomErrorMessageT struct {
Inner []TestCustomErrorMessageInnerT
}
type TestCustomErrorMessageInnerT struct {
Inner2 struct {
Inner3 string `validate:"required"`
}
}
func (TestCustomErrorMessageT) ValidationMessages() []CustomError {
return []CustomError{
{"Inner.Inner2.Inner3.required", BasicMessageResolver("Yay custom error message!")},
}
}
func TestCustomErrorMessages(t *testing.T) {
err := JsonValidate(nil, nil, &TestCustomErrorMessageT{Inner: []TestCustomErrorMessageInnerT{{}}})
assert.NotNil(t, err)
typedErr, ok := err.(*ValidationError)
assert.True(t, ok)
assert.Len(t, typedErr.Errors, 1)
firstErr := typedErr.Errors[0]
assert.Equal(t, "Inner.0.Inner2.Inner3", firstErr.Path)
assert.Len(t, firstErr.Errors, 1)
firstValidatorErr := firstErr.Errors[0]
assert.Equal(t, FieldValidatorError{
Rule: "required",
Hint: "required",
Message: "Yay custom error message!",
}, firstValidatorErr)
}