-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidator.go
More file actions
292 lines (273 loc) · 7.86 KB
/
validator.go
File metadata and controls
292 lines (273 loc) · 7.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package newsdataapi
import (
"fmt"
"net/url"
"sort"
"strconv"
"strings"
)
// Params is the request payload for any endpoint method. Keys are case-
// insensitive (the API normalises to lowercase, and so do we) and values may be
// a string, a []string (comma-joined), a bool, an int, a float, or *bool /
// *float64 if you need to distinguish "unset" from a zero value.
//
// A few keys are interpreted specially and never sent to the API:
//
// "rawQuery" (string) — sent verbatim; mutually exclusive with all other
// params; parsed and validated against the
// endpoint's allowed keys.
//
// See the README for the per-endpoint accepted parameter list.
type Params map[string]any
// validateAndEncode mirrors the Python/PHP/Node client validator: it lowercases
// keys, drops nil/empty values, enforces mutually-exclusive groups, the
// sentiment_score-requires-sentiment rule, the from_date/to_date requirement on
// count endpoints, and the size 1..50 bound. Lists are comma-joined; booleans
// become "1"/"0".
//
// Returns url-encoded values ready to be appended to the request URL.
func validateAndEncode(endpoint string, params Params) (url.Values, error) {
allowed, ok := filters[endpoint]
if !ok {
return nil, &NewsdataValidationError{Message: "unknown endpoint: " + endpoint}
}
// Lowercase keys; drop nil values.
lowered := make(map[string]any, len(params))
for k, v := range params {
if v == nil {
continue
}
lowered[strings.ToLower(k)] = v
}
// rawQuery: mutually exclusive with every other parameter.
if raw, ok := lowered["rawquery"]; ok {
others := make([]string, 0, len(lowered)-1)
for k := range lowered {
if k != "rawquery" {
others = append(others, k)
}
}
if len(others) > 0 {
sort.Strings(others)
return nil, &NewsdataValidationError{
Param: "rawQuery",
Message: fmt.Sprintf("rawQuery cannot be combined with other parameters; got rawQuery and %v", others),
}
}
rawStr, ok := raw.(string)
if !ok {
return nil, &NewsdataValidationError{Param: "rawQuery", Message: "rawQuery must be a string"}
}
return parseRawQuery(rawStr, allowed)
}
// Count endpoints require an explicit date range.
if requiresDateRange[endpoint] {
for _, required := range []string{"from_date", "to_date"} {
if v, ok := lowered[required]; !ok || isEmptyString(v) {
return nil, &NewsdataValidationError{
Param: required,
Message: required + " is required for the " + endpoint + " endpoint",
}
}
}
}
// Mutually-exclusive groups.
for _, group := range mutexGroups {
set := []string{}
for _, name := range group {
if _, ok := lowered[name]; ok {
set = append(set, name)
}
}
if len(set) > 1 {
return nil, &NewsdataValidationError{
Param: set[0],
Message: fmt.Sprintf("these parameters are mutually exclusive: %v", set),
}
}
}
// sentiment_score requires sentiment.
if _, hasScore := lowered["sentiment_score"]; hasScore {
if _, hasSentiment := lowered["sentiment"]; !hasSentiment {
return nil, &NewsdataValidationError{
Param: "sentiment_score",
Message: "sentiment_score requires sentiment to be set",
}
}
}
// Per-param validation + coercion.
out := url.Values{}
for name, value := range lowered {
if !allowed[name] {
return nil, &NewsdataValidationError{
Param: name,
Message: "unsupported parameter for the " + endpoint + " endpoint",
}
}
encoded, err := coerce(name, value)
if err != nil {
return nil, err
}
out.Set(name, encoded)
}
return out, nil
}
func coerce(name string, value any) (string, error) {
switch {
case boolParams[name]:
return coerceBool(name, value)
case intParams[name]:
return coerceInt(name, value)
case floatParams[name]:
return coerceFloat(name, value)
default:
return coerceString(name, value)
}
}
func coerceBool(name string, value any) (string, error) {
switch v := value.(type) {
case bool:
if v {
return "1", nil
}
return "0", nil
case *bool:
if v == nil {
return "", &NewsdataValidationError{Param: name, Message: "must be a boolean"}
}
if *v {
return "1", nil
}
return "0", nil
case int:
if v == 0 {
return "0", nil
}
if v == 1 {
return "1", nil
}
case string:
switch strings.ToLower(strings.TrimSpace(v)) {
case "1", "true", "yes":
return "1", nil
case "0", "false", "no":
return "0", nil
}
}
return "", &NewsdataValidationError{Param: name, Message: "must be a boolean"}
}
func coerceInt(name string, value any) (string, error) {
var n int
switch v := value.(type) {
case int:
n = v
case int32:
n = int(v)
case int64:
n = int(v)
case string:
x, err := strconv.Atoi(v)
if err != nil {
return "", &NewsdataValidationError{Param: name, Message: name + " must be an integer"}
}
n = x
default:
return "", &NewsdataValidationError{Param: name, Message: name + " must be an integer"}
}
if name == "size" && (n < sizeMin || n > sizeMax) {
return "", &NewsdataValidationError{
Param: "size",
Message: fmt.Sprintf("size must be between %d and %d (got %d)", sizeMin, sizeMax, n),
}
}
return strconv.Itoa(n), nil
}
func coerceFloat(name string, value any) (string, error) {
switch v := value.(type) {
case float32:
return strconv.FormatFloat(float64(v), 'f', -1, 32), nil
case float64:
return strconv.FormatFloat(v, 'f', -1, 64), nil
case *float64:
if v == nil {
return "", &NewsdataValidationError{Param: name, Message: name + " must be a number"}
}
return strconv.FormatFloat(*v, 'f', -1, 64), nil
case int:
return strconv.Itoa(v), nil
case string:
if _, err := strconv.ParseFloat(v, 64); err != nil {
return "", &NewsdataValidationError{Param: name, Message: name + " must be a number"}
}
return v, nil
}
return "", &NewsdataValidationError{Param: name, Message: name + " must be a number"}
}
func coerceString(name string, value any) (string, error) {
switch v := value.(type) {
case string:
return v, nil
case []string:
return strings.Join(v, ","), nil
case []any:
parts := make([]string, 0, len(v))
for _, item := range v {
s, ok := item.(string)
if !ok {
return "", &NewsdataValidationError{Param: name, Message: "all items in " + name + " must be strings"}
}
parts = append(parts, s)
}
return strings.Join(parts, ","), nil
case int:
return strconv.Itoa(v), nil
case int64:
return strconv.FormatInt(v, 10), nil
case float64:
return strconv.FormatFloat(v, 'f', -1, 64), nil
}
return "", &NewsdataValidationError{
Param: name,
Message: name + " must be a string or []string",
}
}
// parseRawQuery parses a query string fragment or full URL into a validated
// url.Values, rejecting unknown keys for the endpoint and stripping any
// embedded apikey.
func parseRawQuery(raw string, allowed map[string]bool) (url.Values, error) {
if raw == "" {
return nil, &NewsdataValidationError{Param: "rawQuery", Message: "rawQuery must be a non-empty string"}
}
queryString := raw
// Full URL? Extract just the query part.
if u, err := url.Parse(raw); err == nil && u.Scheme != "" && u.Host != "" {
queryString = u.RawQuery
}
queryString = strings.TrimPrefix(queryString, "?")
values, err := url.ParseQuery(queryString)
if err != nil {
return nil, &NewsdataValidationError{Param: "rawQuery", Message: "invalid rawQuery: " + err.Error()}
}
out := url.Values{}
for k, vs := range values {
name := strings.ToLower(strings.TrimSpace(k))
if name == "" {
continue
}
if name == "apikey" { // supplied by the client
continue
}
if !allowed[name] {
return nil, &NewsdataValidationError{Param: k, Message: "unknown parameter in rawQuery: " + k}
}
if len(vs) == 0 || vs[0] == "" {
return nil, &NewsdataValidationError{Param: k, Message: "parameter " + k + " in rawQuery must have a value"}
}
out.Set(name, vs[0])
}
return out, nil
}
func isEmptyString(v any) bool {
s, ok := v.(string)
return ok && s == ""
}