-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidatorUnpack.go
More file actions
199 lines (167 loc) · 6.86 KB
/
validatorUnpack.go
File metadata and controls
199 lines (167 loc) · 6.86 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
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
189
190
191
192
193
194
195
196
197
198
199
package validator
import (
"encoding/json"
"fmt"
"io"
"net/http"
"github.com/siherrmann/validator/helper"
"github.com/siherrmann/validator/model"
)
// UnmapOrUnmarshalAndValidate unmarshals given json ([]byte) or given url.Values (from request.Form),
// validates them and updates the given struct.
// It returns an error if the unmapping or validation fails.
//
// It is actually doing the same as UnmapOrUnmarshalValidateAndUpdate, but in another order.
// It does directly update the struct and validates afterwards.
// Normally you would either only use Validate or use UnmapOrUnmarshalValidateAndUpdate for early return on error.
func (r *Validator) UnmapOrUnmarshalAndValidate(request *http.Request, structToUpdate any, tagType ...string) error {
err := request.ParseForm()
if err != nil {
return err
}
if len(request.Form.Encode()) > 0 {
err = r.UnmapAndValidate(request, structToUpdate, tagType...)
} else {
err = r.UnmarshalAndValidate(request, structToUpdate, tagType...)
}
return err
}
// UnmarshalAndValidate unmarshals given json ([]byte) into pointer v.
// It validates the struct by the given tagType.
// It returns an error if the unmarshaling or validation fails.
//
// It is actually doing the same as UnmarshalValidateAndUpdate, but in another order.
// It does directly update the struct and validates afterwards.
// Normally you would either only use Validate or use UnmarshalValidateAndUpdate for early return on error.
func (r *Validator) UnmarshalAndValidate(request *http.Request, structToValidate any, tagType ...string) error {
err := helper.CheckValidPointerToStruct(structToValidate)
if err != nil {
return err
}
var bodyBytes []byte
bodyBytes, err = io.ReadAll(request.Body)
if err != nil {
return err
}
err = json.Unmarshal(bodyBytes, structToValidate)
if err != nil {
return fmt.Errorf("error unmarshaling json: %v", err)
}
err = r.Validate(structToValidate, tagType...)
if err != nil {
return fmt.Errorf("error validating struct: %v", err)
}
return nil
}
// UnmapAndValidate unmaps the url.Values from the request.Form into a JsonMap and puts it into the given struct.
// It validates the struct by the given tagType.
// It returns an error if the unmapping or validation fails.
//
// It is actually doing the same as UnmapValidateAndUpdate, but in another order.
// It does directly update the struct and validates afterwards.
// Normally you would either only use Validate or use UnmapValidateAndUpdate for early return on error.
func (r *Validator) UnmapAndValidate(request *http.Request, structToValidate any, tagType ...string) error {
mapOut, err := helper.UnmapRequestToJsonMap(request)
if err != nil {
return fmt.Errorf("error unmapping form values: %v", err)
}
err = helper.MapJsonMapToStruct(mapOut, structToValidate)
if err != nil {
return fmt.Errorf("error mapping json map to struct: %v", err)
}
err = r.Validate(structToValidate, tagType...)
if err != nil {
return fmt.Errorf("error validating url values: %v", err)
}
return nil
}
// UnmapOrUnmarshalValidateAndUpdate unmarshals given json ([]byte) or given url.Values (from request.Form),
// validates them and updates the given struct.
// It returns an error if the unmapping, validation or update fails.
//
// For more information look at ValidateAndUpdate.
func (r *Validator) UnmapOrUnmarshalValidateAndUpdate(request *http.Request, structToUpdate any, tagType ...string) error {
err := request.ParseForm()
if err != nil {
return err
}
if len(request.Form.Encode()) > 0 {
err = r.UnmapValidateAndUpdate(request, structToUpdate, tagType...)
} else {
err = r.UnmarshalValidateAndUpdate(request, structToUpdate, tagType...)
}
return err
}
// UnmarshalValidateAndUpdate unmarshals given json ([]byte) into pointer v.
// It returns an error if the unmapping, validation or update fails.
//
// For more information look at ValidateAndUpdate.
func (r *Validator) UnmarshalValidateAndUpdate(request *http.Request, structToUpdate any, tagType ...string) error {
mapOut, err := helper.UnmarshalRequestToJsonMap(request)
if err != nil {
return fmt.Errorf("error unmarshaling request body: %v", err)
}
err = r.ValidateAndUpdate(mapOut, structToUpdate, tagType...)
if err != nil {
return fmt.Errorf("error updating struct: %v", err)
}
return nil
}
// UnmapValidateAndUpdate unmaps given url.Values into pointer jsonMap.
// It returns an error if the unmapping, validation or update fails.
//
// For more information look at ValidateAndUpdate.
func (r *Validator) UnmapValidateAndUpdate(request *http.Request, structToUpdate any, tagType ...string) error {
mapOut, err := helper.UnmapRequestToJsonMap(request)
if err != nil {
return fmt.Errorf("error unmapping form values: %v", err)
}
err = r.ValidateAndUpdate(mapOut, structToUpdate, tagType...)
if err != nil {
return fmt.Errorf("error updating struct: %v", err)
}
return nil
}
// UnmapOrUnmarshalValidateAndUpdateWithValidation unmarshals given json ([]byte) or given url.Values (from request.Form).
// It validates the map with the given validations and updates the given map.
// It returns an error if the unmapping, validation or update fails.
func (r *Validator) UnmapOrUnmarshalValidateAndUpdateWithValidation(request *http.Request, mapToUpdate *map[string]any, validations []model.Validation) error {
err := request.ParseForm()
if err != nil {
return err
}
if len(request.Form.Encode()) > 0 {
err = r.UnmapValidateAndUpdateWithValidation(request, mapToUpdate, validations)
} else {
err = r.UnmarshalValidateAndUpdateWithValidation(request, mapToUpdate, validations)
}
return err
}
// UnmarshalValidateAndUpdateWithValidation unmarshals given json ([]byte) into pointer mapToUpdate.
// It validates the map by the given validations and updates it.
// It returns an error if the unmarshaling, validation or update fails.
func (r *Validator) UnmarshalValidateAndUpdateWithValidation(request *http.Request, mapToUpdate *map[string]any, validations []model.Validation) error {
mapOut, err := helper.UnmarshalRequestToJsonMap(request)
if err != nil {
return fmt.Errorf("error unmarshaling request body: %v", err)
}
err = r.ValidateAndUpdateWithValidation(mapOut, mapToUpdate, validations)
if err != nil {
return fmt.Errorf("error updating struct: %v", err)
}
return nil
}
// UnmapValidateAndUpdateWithValidation unmaps given url.Values into pointer jsonMap.
// It validates the map by the given validations and updates it.
// It returns an error if the unmapping, validation or update fails.
func (r *Validator) UnmapValidateAndUpdateWithValidation(request *http.Request, mapToUpdate *map[string]any, validations []model.Validation) error {
mapOut, err := helper.UnmapRequestToJsonMap(request)
if err != nil {
return fmt.Errorf("error unmapping form values: %v", err)
}
err = r.ValidateAndUpdateWithValidation(mapOut, mapToUpdate, validations)
if err != nil {
return fmt.Errorf("error updating struct: %v", err)
}
return nil
}