Debian Codegraph @

errors/parameter_errors.go

raw
1 // Copyright 2023-2026 Princess Beef Heavy Industries, LLC / Dave Shanley
2 // SPDX-License-Identifier: MIT
3
4 package errors
5
6 import (
7 "fmt"
8 "net/url"
9 "strings"
10
11 "github.com/pb33f/libopenapi/datamodel/high/base"
12
13 v3 "github.com/pb33f/libopenapi/datamodel/high/v3"
14
15 "github.com/pb33f/libopenapi-validator/helpers"
16 )
17
18 func IncorrectFormEncoding(param *v3.Parameter, qp *helpers.QueryParam, i int) *ValidationError {
19 specLine, specCol := paramExplodeLineCol(param)
20 return &ValidationError{
21 ValidationType: helpers.ParameterValidation,
22 ValidationSubType: helpers.ParameterValidationQuery,
23 Message: fmt.Sprintf("Query parameter '%s' is not exploded correctly", param.Name),
24 Reason: fmt.Sprintf("The query parameter '%s' has a default or 'form' encoding defined, "+
25 "however the value '%s' is encoded as an object or an array using commas. The contract defines "+
26 "the explode value to set to 'true'", param.Name, qp.Values[i]),
27 SpecLine: specLine,
28 SpecCol: specCol,
29 ParameterName: param.Name,
30 Context: param,
31 HowToFix: fmt.Sprintf(HowToFixParamInvalidFormEncode,
32 helpers.CollapseCSVIntoFormStyle(param.Name, qp.Values[i])),
33 }
34 }
35
36 func IncorrectSpaceDelimiting(param *v3.Parameter, qp *helpers.QueryParam) *ValidationError {
37 specLine, specCol := paramStyleLineCol(param)
38 return &ValidationError{
39 ValidationType: helpers.ParameterValidation,
40 ValidationSubType: helpers.ParameterValidationQuery,
41 Message: fmt.Sprintf("Query parameter '%s' delimited incorrectly", param.Name),
42 Reason: fmt.Sprintf("The query parameter '%s' has 'spaceDelimited' style defined, "+
43 "and explode is defined as false. There are multiple values (%d) supplied, instead of a single"+
44 " space delimited value", param.Name, len(qp.Values)),
45 SpecLine: specLine,
46 SpecCol: specCol,
47 ParameterName: param.Name,
48 Context: param,
49 HowToFix: fmt.Sprintf(HowToFixParamInvalidSpaceDelimitedObjectExplode,
50 helpers.CollapseCSVIntoSpaceDelimitedStyle(param.Name, qp.Values)),
51 }
52 }
53
54 func IncorrectPipeDelimiting(param *v3.Parameter, qp *helpers.QueryParam) *ValidationError {
55 specLine, specCol := paramStyleLineCol(param)
56 return &ValidationError{
57 ValidationType: helpers.ParameterValidation,
58 ValidationSubType: helpers.ParameterValidationQuery,
59 Message: fmt.Sprintf("Query parameter '%s' delimited incorrectly", param.Name),
60 Reason: fmt.Sprintf("The query parameter '%s' has 'pipeDelimited' style defined, "+
61 "and explode is defined as false. There are multiple values (%d) supplied, instead of a single"+
62 " space delimited value", param.Name, len(qp.Values)),
63 SpecLine: specLine,
64 SpecCol: specCol,
65 ParameterName: param.Name,
66 Context: param,
67 HowToFix: fmt.Sprintf(HowToFixParamInvalidPipeDelimitedObjectExplode,
68 helpers.CollapseCSVIntoPipeDelimitedStyle(param.Name, qp.Values)),
69 }
70 }
71
72 func InvalidDeepObject(param *v3.Parameter, qp *helpers.QueryParam) *ValidationError {
73 specLine, specCol := paramStyleLineCol(param)
74 return &ValidationError{
75 ValidationType: helpers.ParameterValidation,
76 ValidationSubType: helpers.ParameterValidationQuery,
77 Message: fmt.Sprintf("Query parameter '%s' is not a valid deepObject", param.Name),
78 Reason: fmt.Sprintf("The query parameter '%s' has the 'deepObject' style defined, "+
79 "There are multiple values (%d) supplied, instead of a single "+
80 "value", param.Name, len(qp.Values)),
81 SpecLine: specLine,
82 SpecCol: specCol,
83 ParameterName: param.Name,
84 Context: param,
85 HowToFix: fmt.Sprintf(HowToFixParamInvalidDeepObjectMultipleValues,
86 helpers.CollapseCSVIntoPipeDelimitedStyle(param.Name, qp.Values)),
87 }
88 }
89
90 func deepObjectPathForError(qp *helpers.QueryParam) string {
91 if qp == nil {
92 return ""
93 }
94 if len(qp.PropertyPath) > 0 {
95 return strings.Join(qp.PropertyPath, ".")
96 }
97 return qp.Property
98 }
99
100 func deepObjectBracketPathForError(qp *helpers.QueryParam) string {
101 if qp == nil {
102 return ""
103 }
104 if len(qp.PropertyPath) > 0 {
105 return strings.Join(qp.PropertyPath, "][")
106 }
107 return qp.Property
108 }
109
110 func InvalidDeepObjectPathConflict(param *v3.Parameter, prefixParam, nestedParam *helpers.QueryParam) *ValidationError {
111 specLine, specCol := paramStyleLineCol(param)
112 prefixPath := deepObjectPathForError(prefixParam)
113 nestedPath := deepObjectPathForError(nestedParam)
114 return &ValidationError{
115 ValidationType: helpers.ParameterValidation,
116 ValidationSubType: helpers.ParameterValidationQuery,
117 Message: fmt.Sprintf("Query parameter '%s' is not a valid deepObject", param.Name),
118 Reason: fmt.Sprintf("The query parameter '%s' has the 'deepObject' style defined, "+
119 "but the property path '%s' is also used as a nested object prefix for '%s'",
120 param.Name, prefixPath, nestedPath),
121 SpecLine: specLine,
122 SpecCol: specCol,
123 ParameterName: param.Name,
124 Context: param,
125 HowToFix: fmt.Sprintf(HowToFixParamInvalidDeepObjectPathConflict,
126 param.Name, deepObjectBracketPathForError(prefixParam),
127 param.Name, deepObjectBracketPathForError(nestedParam)),
128 }
129 }
130
131 func QueryParameterMissing(param *v3.Parameter, pathTemplate string, operation string, renderedSchema string) *ValidationError {
132 keywordLocation := helpers.ConstructParameterJSONPointer(pathTemplate, operation, param.Name, "required")
133 specLine, specCol := paramRequiredLineCol(param)
134
135 return &ValidationError{
136 ValidationType: helpers.ParameterValidation,
137 ValidationSubType: helpers.ParameterValidationQuery,
138 Message: fmt.Sprintf("Query parameter '%s' is missing", param.Name),
139 Reason: fmt.Sprintf("The query parameter '%s' is defined as being required, "+
140 "however it's missing from the requests", param.Name),
141 SpecLine: specLine,
142 SpecCol: specCol,
143 ParameterName: param.Name,
144 HowToFix: HowToFixMissingValue,
145 SchemaValidationErrors: []*SchemaValidationFailure{{
146 Reason: fmt.Sprintf("Required query parameter '%s' is missing", param.Name),
147 FieldName: param.Name,
148 FieldPath: "",
149 InstancePath: []string{},
150 KeywordLocation: keywordLocation,
151 ReferenceSchema: renderedSchema,
152 }},
153 }
154 }
155
156 func HeaderParameterMissing(param *v3.Parameter, pathTemplate string, operation string, renderedSchema string) *ValidationError {
157 escapedPath := strings.ReplaceAll(pathTemplate, "~", "~0")
158 escapedPath = strings.ReplaceAll(escapedPath, "/", "~1")
159 escapedPath = strings.TrimPrefix(escapedPath, "~1")
160 keywordLocation := fmt.Sprintf("/paths/%s/%s/parameters/%s/required", escapedPath, strings.ToLower(operation), param.Name)
161 specLine, specCol := paramRequiredLineCol(param)
162
163 return &ValidationError{
164 ValidationType: helpers.ParameterValidation,
165 ValidationSubType: helpers.ParameterValidationHeader,
166 Message: fmt.Sprintf("Header parameter '%s' is missing", param.Name),
167 Reason: fmt.Sprintf("The header parameter '%s' is defined as being required, "+
168 "however it's missing from the requests", param.Name),
169 SpecLine: specLine,
170 SpecCol: specCol,
171 ParameterName: param.Name,
172 HowToFix: HowToFixMissingValue,
173 SchemaValidationErrors: []*SchemaValidationFailure{{
174 Reason: fmt.Sprintf("Required header parameter '%s' is missing", param.Name),
175 FieldName: param.Name,
176 FieldPath: "",
177 InstancePath: []string{},
178 KeywordLocation: keywordLocation,
179 ReferenceSchema: renderedSchema,
180 }},
181 }
182 }
183
184 func CookieParameterMissing(param *v3.Parameter, pathTemplate string, operation string, renderedSchema string) *ValidationError {
185 keywordLocation := helpers.ConstructParameterJSONPointer(pathTemplate, operation, param.Name, "required")
186 specLine, specCol := paramRequiredLineCol(param)
187
188 return &ValidationError{
189 ValidationType: helpers.ParameterValidation,
190 ValidationSubType: helpers.ParameterValidationCookie,
191 Message: fmt.Sprintf("Cookie parameter '%s' is missing", param.Name),
192 Reason: fmt.Sprintf("The cookie parameter '%s' is defined as being required, "+
193 "however it's missing from the request", param.Name),
194 SpecLine: specLine,
195 SpecCol: specCol,
196 ParameterName: param.Name,
197 HowToFix: HowToFixMissingValue,
198 SchemaValidationErrors: []*SchemaValidationFailure{{
199 Reason: fmt.Sprintf("Required cookie parameter '%s' is missing", param.Name),
200 FieldName: param.Name,
201 FieldPath: "",
202 InstancePath: []string{},
203 KeywordLocation: keywordLocation,
204 ReferenceSchema: renderedSchema,
205 }},
206 }
207 }
208
209 func HeaderParameterCannotBeDecoded(param *v3.Parameter, val string, pathTemplate string, operation string, renderedSchema string) *ValidationError {
210 keywordLocation := helpers.ConstructParameterJSONPointer(pathTemplate, operation, param.Name, "type")
211 specLine, specCol := paramSchemaTypeLineCol(param)
212
213 return &ValidationError{
214 ValidationType: helpers.ParameterValidation,
215 ValidationSubType: helpers.ParameterValidationHeader,
216 Message: fmt.Sprintf("Header parameter '%s' cannot be decoded", param.Name),
217 Reason: fmt.Sprintf("The header parameter '%s' cannot be "+
218 "extracted into an object, '%s' is malformed", param.Name, val),
219 SpecLine: specLine,
220 SpecCol: specCol,
221 ParameterName: param.Name,
222 HowToFix: HowToFixInvalidEncoding,
223 SchemaValidationErrors: []*SchemaValidationFailure{{
224 Reason: fmt.Sprintf("Header value '%s' cannot be decoded as object (malformed encoding)", val),
225 FieldName: param.Name,
226 InstancePath: []string{param.Name},
227 KeywordLocation: keywordLocation,
228 ReferenceSchema: renderedSchema,
229 }},
230 }
231 }
232
233 func IncorrectHeaderParamEnum(param *v3.Parameter, ef string, sch *base.Schema, pathTemplate string, operation string, renderedSchema string) *ValidationError {
234 var enums []string
235 for i := range sch.Enum {
236 enums = append(enums, fmt.Sprint(sch.Enum[i].Value))
237 }
238 validEnums := strings.Join(enums, ", ")
239
240 keywordLocation := helpers.ConstructParameterJSONPointer(pathTemplate, operation, param.Name, "enum")
241 specLine, specCol := paramSchemaEnumLineCol(param)
242
243 return &ValidationError{
244 ValidationType: helpers.ParameterValidation,
245 ValidationSubType: helpers.ParameterValidationHeader,
246 Message: fmt.Sprintf("Header parameter '%s' does not match allowed values", param.Name),
247 Reason: fmt.Sprintf("The header parameter '%s' has pre-defined "+
248 "values set via an enum. The value '%s' is not one of those values.", param.Name, ef),
249 SpecLine: specLine,
250 SpecCol: specCol,
251 ParameterName: param.Name,
252 Context: sch,
253 HowToFix: fmt.Sprintf(HowToFixParamInvalidEnum, ef, validEnums),
254 SchemaValidationErrors: []*SchemaValidationFailure{{
255 Reason: fmt.Sprintf("Value '%s' does not match any enum values: [%s]", ef, validEnums),
256 FieldName: param.Name,
257 InstancePath: []string{param.Name},
258 KeywordLocation: keywordLocation,
259 ReferenceSchema: renderedSchema,
260 }},
261 }
262 }
263
264 func IncorrectQueryParamArrayBoolean(
265 param *v3.Parameter, item string, sch *base.Schema, itemsSchema *base.Schema, pathTemplate string, operation string, renderedItemsSchema string,
266 ) *ValidationError {
267 keywordLocation := helpers.ConstructParameterJSONPointer(pathTemplate, operation, param.Name, "items/type")
268 specLine, specCol := schemaItemsTypeLineCol(sch)
269
270 return &ValidationError{
271 ValidationType: helpers.ParameterValidation,
272 ValidationSubType: helpers.ParameterValidationQuery,
273 Message: fmt.Sprintf("Query array parameter '%s' is not a valid boolean", param.Name),
274 Reason: fmt.Sprintf("The query parameter (which is an array) '%s' is defined as being a boolean, "+
275 "however the value '%s' is not a valid true/false value", param.Name, item),
276 SpecLine: specLine,
277 SpecCol: specCol,
278 ParameterName: param.Name,
279 Context: itemsSchema,
280 HowToFix: fmt.Sprintf(HowToFixParamInvalidBoolean, item),
281 SchemaValidationErrors: []*SchemaValidationFailure{{
282 Reason: fmt.Sprintf("Array item '%s' is not a valid boolean", item),
283 FieldName: param.Name,
284 InstancePath: []string{param.Name, "[item]"},
285 KeywordLocation: keywordLocation,
286 ReferenceSchema: renderedItemsSchema,
287 }},
288 }
289 }
290
291 func IncorrectParamArrayMaxNumItems(param *v3.Parameter, sch *base.Schema, expected, actual int64, pathTemplate string, operation string, renderedSchema string) *ValidationError {
292 keywordLocation := helpers.ConstructParameterJSONPointer(pathTemplate, operation, param.Name, "maxItems")
293 specLine, specCol := schemaItemsTypeLineCol(sch)
294
295 return &ValidationError{
296 ValidationType: helpers.ParameterValidation,
297 ValidationSubType: helpers.ParameterValidationQuery,
298 Message: fmt.Sprintf("Query array parameter '%s' has too many items", param.Name),
299 Reason: fmt.Sprintf("The query parameter (which is an array) '%s' has a maximum item length of %d, "+
300 "however the request provided %d items", param.Name, expected, actual),
301 SpecLine: specLine,
302 SpecCol: specCol,
303 ParameterName: param.Name,
304 Context: sch,
305 HowToFix: fmt.Sprintf(HowToFixInvalidMaxItems, expected),
306 SchemaValidationErrors: []*SchemaValidationFailure{{
307 Reason: fmt.Sprintf("Array has %d items, but maximum is %d", actual, expected),
308 FieldName: param.Name,
309 InstancePath: []string{param.Name},
310 KeywordLocation: keywordLocation,
311 ReferenceSchema: renderedSchema,
312 }},
313 }
314 }
315
316 func IncorrectParamArrayMinNumItems(param *v3.Parameter, sch *base.Schema, expected, actual int64, pathTemplate string, operation string, renderedSchema string) *ValidationError {
317 keywordLocation := helpers.ConstructParameterJSONPointer(pathTemplate, operation, param.Name, "minItems")
318 specLine, specCol := schemaItemsTypeLineCol(sch)
319
320 return &ValidationError{
321 ValidationType: helpers.ParameterValidation,
322 ValidationSubType: helpers.ParameterValidationQuery,
323 Message: fmt.Sprintf("Query array parameter '%s' does not have enough items", param.Name),
324 Reason: fmt.Sprintf("The query parameter (which is an array) '%s' has a minimum items length of %d, "+
325 "however the request provided %d items", param.Name, expected, actual),
326 SpecLine: specLine,
327 SpecCol: specCol,
328 ParameterName: param.Name,
329 Context: sch,
330 HowToFix: fmt.Sprintf(HowToFixInvalidMinItems, expected),
331 SchemaValidationErrors: []*SchemaValidationFailure{{
332 Reason: fmt.Sprintf("Array has %d items, but minimum is %d", actual, expected),
333 FieldName: param.Name,
334 InstancePath: []string{param.Name},
335 KeywordLocation: keywordLocation,
336 ReferenceSchema: renderedSchema,
337 }},
338 }
339 }
340
341 func IncorrectParamArrayUniqueItems(param *v3.Parameter, sch *base.Schema, duplicates string, pathTemplate string, operation string, renderedSchema string) *ValidationError {
342 keywordLocation := helpers.ConstructParameterJSONPointer(pathTemplate, operation, param.Name, "uniqueItems")
343 specLine, specCol := schemaItemsTypeLineCol(sch)
344
345 return &ValidationError{
346 ValidationType: helpers.ParameterValidation,
347 ValidationSubType: helpers.ParameterValidationQuery,
348 Message: fmt.Sprintf("Query array parameter '%s' contains non-unique items", param.Name),
349 Reason: fmt.Sprintf("The query parameter (which is an array) '%s' contains the following duplicates: '%s'", param.Name, duplicates),
350 SpecLine: specLine,
351 SpecCol: specCol,
352 ParameterName: param.Name,
353 Context: sch,
354 HowToFix: "Ensure the array values are all unique",
355 SchemaValidationErrors: []*SchemaValidationFailure{{
356 Reason: fmt.Sprintf("Array contains duplicate values: %s", duplicates),
357 FieldName: param.Name,
358 InstancePath: []string{param.Name},
359 KeywordLocation: keywordLocation,
360 ReferenceSchema: renderedSchema,
361 }},
362 }
363 }
364
365 func IncorrectCookieParamArrayBoolean(
366 param *v3.Parameter, item string, sch *base.Schema, itemsSchema *base.Schema, pathTemplate string, operation string, renderedItemsSchema string,
367 ) *ValidationError {
368 keywordLocation := helpers.ConstructParameterJSONPointer(pathTemplate, operation, param.Name, "items/type")
369 specLine, specCol := schemaItemsTypeLineCol(sch)
370
371 return &ValidationError{
372 ValidationType: helpers.ParameterValidation,
373 ValidationSubType: helpers.ParameterValidationCookie,
374 Message: fmt.Sprintf("Cookie array parameter '%s' is not a valid boolean", param.Name),
375 Reason: fmt.Sprintf("The cookie parameter (which is an array) '%s' is defined as being a boolean, "+
376 "however the value '%s' is not a valid true/false value", param.Name, item),
377 SpecLine: specLine,
378 SpecCol: specCol,
379 ParameterName: param.Name,
380 Context: itemsSchema,
381 HowToFix: fmt.Sprintf(HowToFixParamInvalidBoolean, item),
382 SchemaValidationErrors: []*SchemaValidationFailure{{
383 Reason: fmt.Sprintf("Array item '%s' is not a valid boolean", item),
384 FieldName: param.Name,
385 InstancePath: []string{param.Name, "[item]"},
386 KeywordLocation: keywordLocation,
387 ReferenceSchema: renderedItemsSchema,
388 }},
389 }
390 }
391
392 func IncorrectQueryParamArrayInteger(
393 param *v3.Parameter, item string, sch *base.Schema, itemsSchema *base.Schema, pathTemplate string, operation string, renderedItemsSchema string,
394 ) *ValidationError {
395 keywordLocation := helpers.ConstructParameterJSONPointer(pathTemplate, operation, param.Name, "items/type")
396 specLine, specCol := schemaItemsTypeLineCol(sch)
397
398 return &ValidationError{
399 ValidationType: helpers.ParameterValidation,
400 ValidationSubType: helpers.ParameterValidationQuery,
401 Message: fmt.Sprintf("Query array parameter '%s' is not a valid integer", param.Name),
402 Reason: fmt.Sprintf("The query parameter (which is an array) '%s' is defined as being an integer, "+
403 "however the value '%s' is not a valid integer", param.Name, item),
404 SpecLine: specLine,
405 SpecCol: specCol,
406 ParameterName: param.Name,
407 Context: itemsSchema,
408 HowToFix: fmt.Sprintf(HowToFixParamInvalidInteger, item),
409 SchemaValidationErrors: []*SchemaValidationFailure{{
410 Reason: fmt.Sprintf("Array item '%s' is not a valid integer", item),
411 FieldName: param.Name,
412 InstancePath: []string{param.Name, "[item]"},
413 KeywordLocation: keywordLocation,
414 ReferenceSchema: renderedItemsSchema,
415 }},
416 }
417 }
418
419 func IncorrectQueryParamArrayNumber(
420 param *v3.Parameter, item string, sch *base.Schema, itemsSchema *base.Schema, pathTemplate string, operation string, renderedItemsSchema string,
421 ) *ValidationError {
422 keywordLocation := helpers.ConstructParameterJSONPointer(pathTemplate, operation, param.Name, "items/type")
423 specLine, specCol := schemaItemsTypeLineCol(sch)
424
425 return &ValidationError{
426 ValidationType: helpers.ParameterValidation,
427 ValidationSubType: helpers.ParameterValidationQuery,
428 Message: fmt.Sprintf("Query array parameter '%s' is not a valid number", param.Name),
429 Reason: fmt.Sprintf("The query parameter (which is an array) '%s' is defined as being a number, "+
430 "however the value '%s' is not a valid number", param.Name, item),
431 SpecLine: specLine,
432 SpecCol: specCol,
433 ParameterName: param.Name,
434 Context: itemsSchema,
435 HowToFix: fmt.Sprintf(HowToFixParamInvalidNumber, item),
436 SchemaValidationErrors: []*SchemaValidationFailure{{
437 Reason: fmt.Sprintf("Array item '%s' is not a valid number", item),
438 FieldName: param.Name,
439 InstancePath: []string{param.Name, "[item]"},
440 KeywordLocation: keywordLocation,
441 ReferenceSchema: renderedItemsSchema,
442 }},
443 }
444 }
445
446 func IncorrectCookieParamArrayNumber(
447 param *v3.Parameter, item string, sch *base.Schema, itemsSchema *base.Schema, pathTemplate string, operation string, renderedItemsSchema string,
448 ) *ValidationError {
449 keywordLocation := helpers.ConstructParameterJSONPointer(pathTemplate, operation, param.Name, "items/type")
450 specLine, specCol := schemaItemsTypeLineCol(sch)
451
452 return &ValidationError{
453 ValidationType: helpers.ParameterValidation,
454 ValidationSubType: helpers.ParameterValidationCookie,
455 Message: fmt.Sprintf("Cookie array parameter '%s' is not a valid number", param.Name),
456 Reason: fmt.Sprintf("The cookie parameter (which is an array) '%s' is defined as being a number, "+
457 "however the value '%s' is not a valid number", param.Name, item),
458 SpecLine: specLine,
459 SpecCol: specCol,
460 ParameterName: param.Name,
461 Context: itemsSchema,
462 HowToFix: fmt.Sprintf(HowToFixParamInvalidNumber, item),
463 SchemaValidationErrors: []*SchemaValidationFailure{{
464 Reason: fmt.Sprintf("Array item '%s' is not a valid number", item),
465 FieldName: param.Name,
466 InstancePath: []string{param.Name, "[item]"},
467 KeywordLocation: keywordLocation,
468 ReferenceSchema: renderedItemsSchema,
469 }},
470 }
471 }
472
473 func IncorrectParamEncodingJSON(param *v3.Parameter, ef string, sch *base.Schema, pathTemplate string, operation string, renderedSchema string) *ValidationError {
474 escapedPath := strings.ReplaceAll(pathTemplate, "~", "~0")
475 escapedPath = strings.ReplaceAll(escapedPath, "/", "~1")
476 escapedPath = strings.TrimPrefix(escapedPath, "~1")
477 keywordLocation := fmt.Sprintf("/paths/%s/%s/parameters/%s/content/application~1json/schema", escapedPath, strings.ToLower(operation), param.Name)
478 specLine, specCol := paramContentLineCol(param, helpers.JSONContentType)
479
480 return &ValidationError{
481 ValidationType: helpers.ParameterValidation,
482 ValidationSubType: helpers.ParameterValidationQuery,
483 Message: fmt.Sprintf("Query parameter '%s' is not valid JSON", param.Name),
484 Reason: fmt.Sprintf("The query parameter '%s' is defined as being a JSON object, "+
485 "however the value '%s' is not valid JSON", param.Name, ef),
486 SpecLine: specLine,
487 SpecCol: specCol,
488 ParameterName: param.Name,
489 Context: sch,
490 HowToFix: HowToFixInvalidJSON,
491 SchemaValidationErrors: []*SchemaValidationFailure{{
492 Reason: fmt.Sprintf("Value '%s' is not valid JSON", ef),
493 FieldName: param.Name,
494 InstancePath: []string{param.Name},
495 KeywordLocation: keywordLocation,
496 ReferenceSchema: renderedSchema,
497 }},
498 }
499 }
500
501 func IncorrectQueryParamBool(param *v3.Parameter, ef string, sch *base.Schema, pathTemplate string, operation string, renderedSchema string) *ValidationError {
502 keywordLocation := helpers.ConstructParameterJSONPointer(pathTemplate, operation, param.Name, "type")
503 specLine, specCol := paramSchemaKeyLineCol(param)
504
505 return &ValidationError{
506 ValidationType: helpers.ParameterValidation,
507 ValidationSubType: helpers.ParameterValidationQuery,
508 Message: fmt.Sprintf("Query parameter '%s' is not a valid boolean", param.Name),
509 Reason: fmt.Sprintf("The query parameter '%s' is defined as being a boolean, "+
510 "however the value '%s' is not a valid boolean", param.Name, ef),
511 SpecLine: specLine,
512 SpecCol: specCol,
513 ParameterName: param.Name,
514 Context: sch,
515 HowToFix: fmt.Sprintf(HowToFixParamInvalidBoolean, ef),
516 SchemaValidationErrors: []*SchemaValidationFailure{{
517 Reason: fmt.Sprintf("Value '%s' is not a valid boolean", ef),
518 FieldName: param.Name,
519 InstancePath: []string{param.Name},
520 KeywordLocation: keywordLocation,
521 ReferenceSchema: renderedSchema,
522 }},
523 }
524 }
525
526 func InvalidQueryParamInteger(param *v3.Parameter, ef string, sch *base.Schema, pathTemplate string, operation string, renderedSchema string) *ValidationError {
527 keywordLocation := helpers.ConstructParameterJSONPointer(pathTemplate, operation, param.Name, "type")
528 specLine, specCol := paramSchemaKeyLineCol(param)
529
530 return &ValidationError{
531 ValidationType: helpers.ParameterValidation,
532 ValidationSubType: helpers.ParameterValidationQuery,
533 Message: fmt.Sprintf("Query parameter '%s' is not a valid integer", param.Name),
534 Reason: fmt.Sprintf("The query parameter '%s' is defined as being an integer, "+
535 "however the value '%s' is not a valid integer", param.Name, ef),
536 SpecLine: specLine,
537 SpecCol: specCol,
538 ParameterName: param.Name,
539 Context: sch,
540 HowToFix: fmt.Sprintf(HowToFixParamInvalidInteger, ef),
541 SchemaValidationErrors: []*SchemaValidationFailure{{
542 Reason: fmt.Sprintf("Value '%s' is not a valid integer", ef),
543 FieldName: param.Name,
544 InstancePath: []string{param.Name},
545 KeywordLocation: keywordLocation,
546 ReferenceSchema: renderedSchema,
547 }},
548 }
549 }
550
551 func InvalidQueryParamNumber(param *v3.Parameter, ef string, sch *base.Schema, pathTemplate string, operation string, renderedSchema string) *ValidationError {
552 keywordLocation := helpers.ConstructParameterJSONPointer(pathTemplate, operation, param.Name, "type")
553 specLine, specCol := paramSchemaKeyLineCol(param)
554
555 return &ValidationError{
556 ValidationType: helpers.ParameterValidation,
557 ValidationSubType: helpers.ParameterValidationQuery,
558 Message: fmt.Sprintf("Query parameter '%s' is not a valid number", param.Name),
559 Reason: fmt.Sprintf("The query parameter '%s' is defined as being a number, "+
560 "however the value '%s' is not a valid number", param.Name, ef),
561 SpecLine: specLine,
562 SpecCol: specCol,
563 ParameterName: param.Name,
564 Context: sch,
565 HowToFix: fmt.Sprintf(HowToFixParamInvalidNumber, ef),
566 SchemaValidationErrors: []*SchemaValidationFailure{{
567 Reason: fmt.Sprintf("Value '%s' is not a valid number", ef),
568 FieldName: param.Name,
569 InstancePath: []string{param.Name},
570 KeywordLocation: keywordLocation,
571 ReferenceSchema: renderedSchema,
572 }},
573 }
574 }
575
576 func IncorrectQueryParamEnum(param *v3.Parameter, ef string, sch *base.Schema, pathTemplate string, operation string, renderedSchema string) *ValidationError {
577 var enums []string
578 for i := range sch.Enum {
579 enums = append(enums, fmt.Sprint(sch.Enum[i].Value))
580 }
581 validEnums := strings.Join(enums, ", ")
582
583 keywordLocation := helpers.ConstructParameterJSONPointer(pathTemplate, operation, param.Name, "enum")
584 specLine, specCol := paramSchemaEnumLineCol(param)
585
586 return &ValidationError{
587 ValidationType: helpers.ParameterValidation,
588 ValidationSubType: helpers.ParameterValidationQuery,
589 Message: fmt.Sprintf("Query parameter '%s' does not match allowed values", param.Name),
590 Reason: fmt.Sprintf("The query parameter '%s' has pre-defined "+
591 "values set via an enum. The value '%s' is not one of those values.", param.Name, ef),
592 SpecLine: specLine,
593 SpecCol: specCol,
594 ParameterName: param.Name,
595 Context: sch,
596 HowToFix: fmt.Sprintf(HowToFixParamInvalidEnum, ef, validEnums),
597 SchemaValidationErrors: []*SchemaValidationFailure{{
598 Reason: fmt.Sprintf("Value '%s' does not match any enum values: [%s]", ef, validEnums),
599 FieldName: param.Name,
600 InstancePath: []string{param.Name},
601 KeywordLocation: keywordLocation,
602 ReferenceSchema: renderedSchema,
603 }},
604 }
605 }
606
607 func IncorrectQueryParamEnumArray(param *v3.Parameter, ef string, sch *base.Schema, pathTemplate string, operation string, renderedItemsSchema string) *ValidationError {
608 var enums []string
609 if low := param.GoLow(); low != nil {
610 if sv := low.Schema.Value; sv != nil {
611 if s := sv.Schema(); s != nil {
612 if iv := s.Items.Value; iv != nil {
613 if as := iv.A.Schema(); as != nil {
614 for i := range as.Enum.Value {
615 enums = append(enums, fmt.Sprint(as.Enum.Value[i].Value.Value))
616 }
617 }
618 }
619 }
620 }
621 }
622 validEnums := strings.Join(enums, ", ")
623
624 keywordLocation := helpers.ConstructParameterJSONPointer(pathTemplate, operation, param.Name, "items/enum")
625
626 specLine, specCol := 1, 0
627 if low := param.GoLow(); low != nil {
628 if sv := low.Schema.Value; sv != nil {
629 if s := sv.Schema(); s != nil {
630 if iv := s.Items.Value; iv != nil {
631 if as := iv.A.Schema(); as != nil && as.Enum.KeyNode != nil {
632 specLine = as.Enum.KeyNode.Line
633 specCol = as.Enum.KeyNode.Column
634 }
635 }
636 }
637 }
638 }
639
640 return &ValidationError{
641 ValidationType: helpers.ParameterValidation,
642 ValidationSubType: helpers.ParameterValidationQuery,
643 Message: fmt.Sprintf("Query array parameter '%s' does not match allowed values", param.Name),
644 Reason: fmt.Sprintf("The query array parameter '%s' has pre-defined "+
645 "values set via an enum. The value '%s' is not one of those values.", param.Name, ef),
646 SpecLine: specLine,
647 SpecCol: specCol,
648 ParameterName: param.Name,
649 Context: sch,
650 HowToFix: fmt.Sprintf(HowToFixParamInvalidEnum, ef, validEnums),
651 SchemaValidationErrors: []*SchemaValidationFailure{{
652 Reason: fmt.Sprintf("Array item '%s' does not match any enum values: [%s]", ef, validEnums),
653 FieldName: param.Name,
654 InstancePath: []string{param.Name, "[item]"},
655 KeywordLocation: keywordLocation,
656 ReferenceSchema: renderedItemsSchema,
657 }},
658 }
659 }
660
661 func IncorrectReservedValues(param *v3.Parameter, ef string, sch *base.Schema, pathTemplate string, operation string, renderedSchema string) *ValidationError {
662 escapedPath := strings.ReplaceAll(pathTemplate, "~", "~0")
663 escapedPath = strings.ReplaceAll(escapedPath, "/", "~1")
664 escapedPath = strings.TrimPrefix(escapedPath, "~1")
665 keywordLocation := fmt.Sprintf("/paths/%s/%s/parameters/%s/allowReserved", escapedPath, strings.ToLower(operation), param.Name)
666 specLine, specCol := paramSchemaKeyLineCol(param)
667
668 return &ValidationError{
669 ValidationType: helpers.ParameterValidation,
670 ValidationSubType: helpers.ParameterValidationQuery,
671 Message: fmt.Sprintf("Query parameter '%s' value contains reserved values", param.Name),
672 Reason: fmt.Sprintf("The query parameter '%s' has 'allowReserved' set to false, "+
673 "however the value '%s' contains one of the following characters: :/?#[]@!$&'()*+,;=", param.Name, ef),
674 SpecLine: specLine,
675 SpecCol: specCol,
676 ParameterName: param.Name,
677 Context: sch,
678 HowToFix: fmt.Sprintf(HowToFixReservedValues, url.QueryEscape(ef)),
679 SchemaValidationErrors: []*SchemaValidationFailure{{
680 Reason: fmt.Sprintf("Value '%s' contains reserved characters but allowReserved is false", ef),
681 FieldName: param.Name,
682 InstancePath: []string{param.Name},
683 KeywordLocation: keywordLocation,
684 ReferenceSchema: renderedSchema,
685 }},
686 }
687 }
688
689 func InvalidHeaderParamInteger(param *v3.Parameter, ef string, sch *base.Schema, pathTemplate string, operation string, renderedSchema string) *ValidationError {
690 keywordLocation := helpers.ConstructParameterJSONPointer(pathTemplate, operation, param.Name, "type")
691 specLine, specCol := paramSchemaKeyLineCol(param)
692
693 return &ValidationError{
694 ValidationType: helpers.ParameterValidation,
695 ValidationSubType: helpers.ParameterValidationHeader,
696 Message: fmt.Sprintf("Header parameter '%s' is not a valid integer", param.Name),
697 Reason: fmt.Sprintf("The header parameter '%s' is defined as being an integer, "+
698 "however the value '%s' is not a valid integer", param.Name, ef),
699 SpecLine: specLine,
700 SpecCol: specCol,
701 ParameterName: param.Name,
702 Context: sch,
703 HowToFix: fmt.Sprintf(HowToFixParamInvalidInteger, ef),
704 SchemaValidationErrors: []*SchemaValidationFailure{{
705 Reason: fmt.Sprintf("Value '%s' is not a valid integer", ef),
706 FieldName: param.Name,
707 InstancePath: []string{param.Name},
708 KeywordLocation: keywordLocation,
709 ReferenceSchema: renderedSchema,
710 }},
711 }
712 }
713
714 func InvalidHeaderParamNumber(param *v3.Parameter, ef string, sch *base.Schema, pathTemplate string, operation string, renderedSchema string) *ValidationError {
715 keywordLocation := helpers.ConstructParameterJSONPointer(pathTemplate, operation, param.Name, "type")
716 specLine, specCol := paramSchemaKeyLineCol(param)
717
718 return &ValidationError{
719 ValidationType: helpers.ParameterValidation,
720 ValidationSubType: helpers.ParameterValidationHeader,
721 Message: fmt.Sprintf("Header parameter '%s' is not a valid number", param.Name),
722 Reason: fmt.Sprintf("The header parameter '%s' is defined as being a number, "+
723 "however the value '%s' is not a valid number", param.Name, ef),
724 SpecLine: specLine,
725 SpecCol: specCol,
726 ParameterName: param.Name,
727 Context: sch,
728 HowToFix: fmt.Sprintf(HowToFixParamInvalidNumber, ef),
729 SchemaValidationErrors: []*SchemaValidationFailure{{
730 Reason: fmt.Sprintf("Value '%s' is not a valid number", ef),
731 FieldName: param.Name,
732 InstancePath: []string{param.Name},
733 KeywordLocation: keywordLocation,
734 ReferenceSchema: renderedSchema,
735 }},
736 }
737 }
738
739 func InvalidCookieParamInteger(param *v3.Parameter, ef string, sch *base.Schema, pathTemplate string, operation string, renderedSchema string) *ValidationError {
740 keywordLocation := helpers.ConstructParameterJSONPointer(pathTemplate, operation, param.Name, "type")
741 specLine, specCol := paramSchemaKeyLineCol(param)
742
743 return &ValidationError{
744 ValidationType: helpers.ParameterValidation,
745 ValidationSubType: helpers.ParameterValidationCookie,
746 Message: fmt.Sprintf("Cookie parameter '%s' is not a valid integer", param.Name),
747 Reason: fmt.Sprintf("The cookie parameter '%s' is defined as being an integer, "+
748 "however the value '%s' is not a valid integer", param.Name, ef),
749 SpecLine: specLine,
750 SpecCol: specCol,
751 ParameterName: param.Name,
752 Context: sch,
753 HowToFix: fmt.Sprintf(HowToFixParamInvalidInteger, ef),
754 SchemaValidationErrors: []*SchemaValidationFailure{{
755 Reason: fmt.Sprintf("Value '%s' is not a valid integer", ef),
756 FieldName: param.Name,
757 InstancePath: []string{param.Name},
758 KeywordLocation: keywordLocation,
759 ReferenceSchema: renderedSchema,
760 }},
761 }
762 }
763
764 func InvalidCookieParamNumber(param *v3.Parameter, ef string, sch *base.Schema, pathTemplate string, operation string, renderedSchema string) *ValidationError {
765 keywordLocation := helpers.ConstructParameterJSONPointer(pathTemplate, operation, param.Name, "type")
766 specLine, specCol := paramSchemaKeyLineCol(param)
767
768 return &ValidationError{
769 ValidationType: helpers.ParameterValidation,
770 ValidationSubType: helpers.ParameterValidationCookie,
771 Message: fmt.Sprintf("Cookie parameter '%s' is not a valid number", param.Name),
772 Reason: fmt.Sprintf("The cookie parameter '%s' is defined as being a number, "+
773 "however the value '%s' is not a valid number", param.Name, ef),
774 SpecLine: specLine,
775 SpecCol: specCol,
776 ParameterName: param.Name,
777 Context: sch,
778 HowToFix: fmt.Sprintf(HowToFixParamInvalidNumber, ef),
779 SchemaValidationErrors: []*SchemaValidationFailure{{
780 Reason: fmt.Sprintf("Value '%s' is not a valid number", ef),
781 FieldName: param.Name,
782 InstancePath: []string{param.Name},
783 KeywordLocation: keywordLocation,
784 ReferenceSchema: renderedSchema,
785 }},
786 }
787 }
788
789 func IncorrectHeaderParamBool(param *v3.Parameter, ef string, sch *base.Schema, pathTemplate string, operation string, renderedSchema string) *ValidationError {
790 keywordLocation := helpers.ConstructParameterJSONPointer(pathTemplate, operation, param.Name, "type")
791 specLine, specCol := paramSchemaKeyLineCol(param)
792
793 return &ValidationError{
794 ValidationType: helpers.ParameterValidation,
795 ValidationSubType: helpers.ParameterValidationHeader,
796 Message: fmt.Sprintf("Header parameter '%s' is not a valid boolean", param.Name),
797 Reason: fmt.Sprintf("The header parameter '%s' is defined as being a boolean, "+
798 "however the value '%s' is not a valid boolean", param.Name, ef),
799 SpecLine: specLine,
800 SpecCol: specCol,
801 ParameterName: param.Name,
802 Context: sch,
803 HowToFix: fmt.Sprintf(HowToFixParamInvalidBoolean, ef),
804 SchemaValidationErrors: []*SchemaValidationFailure{{
805 Reason: fmt.Sprintf("Value '%s' is not a valid boolean", ef),
806 FieldName: param.Name,
807 InstancePath: []string{param.Name},
808 KeywordLocation: keywordLocation,
809 ReferenceSchema: renderedSchema,
810 }},
811 }
812 }
813
814 func IncorrectCookieParamBool(param *v3.Parameter, ef string, sch *base.Schema, pathTemplate string, operation string, renderedSchema string) *ValidationError {
815 keywordLocation := helpers.ConstructParameterJSONPointer(pathTemplate, operation, param.Name, "type")
816 specLine, specCol := paramSchemaKeyLineCol(param)
817
818 return &ValidationError{
819 ValidationType: helpers.ParameterValidation,
820 ValidationSubType: helpers.ParameterValidationCookie,
821 Message: fmt.Sprintf("Cookie parameter '%s' is not a valid boolean", param.Name),
822 Reason: fmt.Sprintf("The cookie parameter '%s' is defined as being a boolean, "+
823 "however the value '%s' is not a valid boolean", param.Name, ef),
824 SpecLine: specLine,
825 SpecCol: specCol,
826 ParameterName: param.Name,
827 Context: sch,
828 HowToFix: fmt.Sprintf(HowToFixParamInvalidBoolean, ef),
829 SchemaValidationErrors: []*SchemaValidationFailure{{
830 Reason: fmt.Sprintf("Value '%s' is not a valid boolean", ef),
831 FieldName: param.Name,
832 InstancePath: []string{param.Name},
833 KeywordLocation: keywordLocation,
834 ReferenceSchema: renderedSchema,
835 }},
836 }
837 }
838
839 func IncorrectCookieParamEnum(param *v3.Parameter, ef string, sch *base.Schema, pathTemplate string, operation string, renderedSchema string) *ValidationError {
840 var enums []string
841 for i := range sch.Enum {
842 enums = append(enums, fmt.Sprint(sch.Enum[i].Value))
843 }
844 validEnums := strings.Join(enums, ", ")
845
846 keywordLocation := helpers.ConstructParameterJSONPointer(pathTemplate, operation, param.Name, "enum")
847 specLine, specCol := paramSchemaEnumLineCol(param)
848
849 return &ValidationError{
850 ValidationType: helpers.ParameterValidation,
851 ValidationSubType: helpers.ParameterValidationCookie,
852 Message: fmt.Sprintf("Cookie parameter '%s' does not match allowed values", param.Name),
853 Reason: fmt.Sprintf("The cookie parameter '%s' has pre-defined "+
854 "values set via an enum. The value '%s' is not one of those values.", param.Name, ef),
855 SpecLine: specLine,
856 SpecCol: specCol,
857 ParameterName: param.Name,
858 Context: sch,
859 HowToFix: fmt.Sprintf(HowToFixParamInvalidEnum, ef, validEnums),
860 SchemaValidationErrors: []*SchemaValidationFailure{{
861 Reason: fmt.Sprintf("Value '%s' does not match any enum values: [%s]", ef, validEnums),
862 FieldName: param.Name,
863 InstancePath: []string{param.Name},
864 KeywordLocation: keywordLocation,
865 ReferenceSchema: renderedSchema,
866 }},
867 }
868 }
869
870 func IncorrectHeaderParamArrayBoolean(
871 param *v3.Parameter, item string, sch *base.Schema, itemsSchema *base.Schema, pathTemplate string, operation string, renderedItemsSchema string,
872 ) *ValidationError {
873 keywordLocation := helpers.ConstructParameterJSONPointer(pathTemplate, operation, param.Name, "items/type")
874 specLine, specCol := schemaItemsTypeLineCol(sch)
875
876 return &ValidationError{
877 ValidationType: helpers.ParameterValidation,
878 ValidationSubType: helpers.ParameterValidationHeader,
879 Message: fmt.Sprintf("Header array parameter '%s' is not a valid boolean", param.Name),
880 Reason: fmt.Sprintf("The header parameter (which is an array) '%s' is defined as being a boolean, "+
881 "however the value '%s' is not a valid true/false value", param.Name, item),
882 SpecLine: specLine,
883 SpecCol: specCol,
884 ParameterName: param.Name,
885 Context: itemsSchema,
886 HowToFix: fmt.Sprintf(HowToFixParamInvalidBoolean, item),
887 SchemaValidationErrors: []*SchemaValidationFailure{{
888 Reason: fmt.Sprintf("Array item '%s' is not a valid boolean", item),
889 FieldName: param.Name,
890 InstancePath: []string{param.Name, "[item]"},
891 KeywordLocation: keywordLocation,
892 ReferenceSchema: renderedItemsSchema,
893 }},
894 }
895 }
896
897 func IncorrectHeaderParamArrayNumber(
898 param *v3.Parameter, item string, sch *base.Schema, itemsSchema *base.Schema, pathTemplate string, operation string, renderedItemsSchema string,
899 ) *ValidationError {
900 keywordLocation := helpers.ConstructParameterJSONPointer(pathTemplate, operation, param.Name, "items/type")
901 specLine, specCol := schemaItemsTypeLineCol(sch)
902
903 return &ValidationError{
904 ValidationType: helpers.ParameterValidation,
905 ValidationSubType: helpers.ParameterValidationHeader,
906 Message: fmt.Sprintf("Header array parameter '%s' is not a valid number", param.Name),
907 Reason: fmt.Sprintf("The header parameter (which is an array) '%s' is defined as being a number, "+
908 "however the value '%s' is not a valid number", param.Name, item),
909 SpecLine: specLine,
910 SpecCol: specCol,
911 ParameterName: param.Name,
912 Context: itemsSchema,
913 HowToFix: fmt.Sprintf(HowToFixParamInvalidNumber, item),
914 SchemaValidationErrors: []*SchemaValidationFailure{{
915 Reason: fmt.Sprintf("Array item '%s' is not a valid number", item),
916 FieldName: param.Name,
917 InstancePath: []string{param.Name, "[item]"},
918 KeywordLocation: keywordLocation,
919 ReferenceSchema: renderedItemsSchema,
920 }},
921 }
922 }
923
924 func IncorrectPathParamBool(param *v3.Parameter, item string, sch *base.Schema, pathTemplate string, renderedSchema string) *ValidationError {
925 escapedPath := strings.ReplaceAll(pathTemplate, "~", "~0")
926 escapedPath = strings.ReplaceAll(escapedPath, "/", "~1")
927 keywordLocation := fmt.Sprintf("/paths/%s/parameters/%s/schema/type", escapedPath, param.Name)
928 specLine, specCol := paramSchemaKeyLineCol(param)
929
930 return &ValidationError{
931 ValidationType: helpers.ParameterValidation,
932 ValidationSubType: helpers.ParameterValidationPath,
933 Message: fmt.Sprintf("Path parameter '%s' is not a valid boolean", param.Name),
934 Reason: fmt.Sprintf("The path parameter '%s' is defined as being a boolean, "+
935 "however the value '%s' is not a valid boolean", param.Name, item),
936 SpecLine: specLine,
937 SpecCol: specCol,
938 ParameterName: param.Name,
939 Context: sch,
940 HowToFix: fmt.Sprintf(HowToFixParamInvalidBoolean, item),
941 SchemaValidationErrors: []*SchemaValidationFailure{{
942 Reason: fmt.Sprintf("Value '%s' is not a valid boolean", item),
943 FieldName: param.Name,
944 InstancePath: []string{param.Name},
945 KeywordLocation: keywordLocation,
946 ReferenceSchema: renderedSchema,
947 }},
948 }
949 }
950
951 func IncorrectPathParamEnum(param *v3.Parameter, ef string, sch *base.Schema, pathTemplate string, renderedSchema string) *ValidationError {
952 escapedPath := strings.ReplaceAll(pathTemplate, "~", "~0")
953 escapedPath = strings.ReplaceAll(escapedPath, "/", "~1")
954 keywordLocation := fmt.Sprintf("/paths/%s/parameters/%s/schema/enum", escapedPath, param.Name)
955
956 var enums []string
957 for i := range sch.Enum {
958 enums = append(enums, fmt.Sprint(sch.Enum[i].Value))
959 }
960 validEnums := strings.Join(enums, ", ")
961 specLine, specCol := paramSchemaEnumLineCol(param)
962
963 return &ValidationError{
964 ValidationType: helpers.ParameterValidation,
965 ValidationSubType: helpers.ParameterValidationPath,
966 ParameterName: param.Name,
967 Message: fmt.Sprintf("Path parameter '%s' does not match allowed values", param.Name),
968 Reason: fmt.Sprintf("The path parameter '%s' has pre-defined "+
969 "values set via an enum. The value '%s' is not one of those values.", param.Name, ef),
970 SpecLine: specLine,
971 SpecCol: specCol,
972 Context: sch,
973 HowToFix: fmt.Sprintf(HowToFixParamInvalidEnum, ef, validEnums),
974 SchemaValidationErrors: []*SchemaValidationFailure{{
975 Reason: fmt.Sprintf("Value '%s' does not match any enum values: [%s]", ef, validEnums),
976 FieldName: param.Name,
977 InstancePath: []string{param.Name},
978 KeywordLocation: keywordLocation,
979 ReferenceSchema: renderedSchema,
980 }},
981 }
982 }
983
984 func IncorrectPathParamInteger(param *v3.Parameter, item string, sch *base.Schema, pathTemplate string, renderedSchema string) *ValidationError {
985 escapedPath := strings.ReplaceAll(pathTemplate, "~", "~0")
986 escapedPath = strings.ReplaceAll(escapedPath, "/", "~1")
987 keywordLocation := fmt.Sprintf("/paths/%s/parameters/%s/schema/type", escapedPath, param.Name)
988 specLine, specCol := paramSchemaKeyLineCol(param)
989
990 return &ValidationError{
991 ValidationType: helpers.ParameterValidation,
992 ValidationSubType: helpers.ParameterValidationPath,
993 Message: fmt.Sprintf("Path parameter '%s' is not a valid integer", param.Name),
994 Reason: fmt.Sprintf("The path parameter '%s' is defined as being an integer, "+
995 "however the value '%s' is not a valid integer", param.Name, item),
996 SpecLine: specLine,
997 SpecCol: specCol,
998 ParameterName: param.Name,
999 Context: sch,
1000 HowToFix: fmt.Sprintf(HowToFixParamInvalidInteger, item),
1001 SchemaValidationErrors: []*SchemaValidationFailure{{
1002 Reason: fmt.Sprintf("Value '%s' is not a valid integer", item),
1003 FieldName: param.Name,
1004 InstancePath: []string{param.Name},
1005 KeywordLocation: keywordLocation,
1006 ReferenceSchema: renderedSchema,
1007 }},
1008 }
1009 }
1010
1011 func IncorrectPathParamNumber(param *v3.Parameter, item string, sch *base.Schema, pathTemplate string, renderedSchema string) *ValidationError {
1012 escapedPath := strings.ReplaceAll(pathTemplate, "~", "~0")
1013 escapedPath = strings.ReplaceAll(escapedPath, "/", "~1")
1014 keywordLocation := fmt.Sprintf("/paths/%s/parameters/%s/schema/type", escapedPath, param.Name)
1015 specLine, specCol := paramSchemaKeyLineCol(param)
1016
1017 return &ValidationError{
1018 ValidationType: helpers.ParameterValidation,
1019 ValidationSubType: helpers.ParameterValidationPath,
1020 Message: fmt.Sprintf("Path parameter '%s' is not a valid number", param.Name),
1021 Reason: fmt.Sprintf("The path parameter '%s' is defined as being a number, "+
1022 "however the value '%s' is not a valid number", param.Name, item),
1023 SpecLine: specLine,
1024 SpecCol: specCol,
1025 ParameterName: param.Name,
1026 Context: sch,
1027 HowToFix: fmt.Sprintf(HowToFixParamInvalidNumber, item),
1028 SchemaValidationErrors: []*SchemaValidationFailure{{
1029 Reason: fmt.Sprintf("Value '%s' is not a valid number", item),
1030 FieldName: param.Name,
1031 InstancePath: []string{param.Name},
1032 KeywordLocation: keywordLocation,
1033 ReferenceSchema: renderedSchema,
1034 }},
1035 }
1036 }
1037
1038 func IncorrectPathParamArrayNumber(
1039 param *v3.Parameter, item string, sch *base.Schema, itemsSchema *base.Schema, pathTemplate string, renderedSchema string,
1040 ) *ValidationError {
1041 escapedPath := strings.ReplaceAll(pathTemplate, "~", "~0")
1042 escapedPath = strings.ReplaceAll(escapedPath, "/", "~1")
1043 keywordLocation := fmt.Sprintf("/paths/%s/parameters/%s/schema/items/type", escapedPath, param.Name)
1044 specLine, specCol := schemaItemsTypeLineCol(sch)
1045
1046 return &ValidationError{
1047 ValidationType: helpers.ParameterValidation,
1048 ValidationSubType: helpers.ParameterValidationPath,
1049 Message: fmt.Sprintf("Path array parameter '%s' is not a valid number", param.Name),
1050 Reason: fmt.Sprintf("The path parameter (which is an array) '%s' is defined as being a number, "+
1051 "however the value '%s' is not a valid number", param.Name, item),
1052 SpecLine: specLine,
1053 SpecCol: specCol,
1054 ParameterName: param.Name,
1055 Context: itemsSchema,
1056 HowToFix: fmt.Sprintf(HowToFixParamInvalidNumber, item),
1057 SchemaValidationErrors: []*SchemaValidationFailure{{
1058 Reason: fmt.Sprintf("Array item '%s' is not a valid number", item),
1059 FieldName: param.Name,
1060 InstancePath: []string{param.Name, "[item]"},
1061 KeywordLocation: keywordLocation,
1062 ReferenceSchema: renderedSchema,
1063 }},
1064 }
1065 }
1066
1067 func IncorrectPathParamArrayInteger(
1068 param *v3.Parameter, item string, sch *base.Schema, itemsSchema *base.Schema, pathTemplate string, renderedSchema string,
1069 ) *ValidationError {
1070 escapedPath := strings.ReplaceAll(pathTemplate, "~", "~0")
1071 escapedPath = strings.ReplaceAll(escapedPath, "/", "~1")
1072 keywordLocation := fmt.Sprintf("/paths/%s/parameters/%s/schema/items/type", escapedPath, param.Name)
1073 specLine, specCol := schemaItemsTypeLineCol(sch)
1074
1075 return &ValidationError{
1076 ValidationType: helpers.ParameterValidation,
1077 ValidationSubType: helpers.ParameterValidationPath,
1078 Message: fmt.Sprintf("Path array parameter '%s' is not a valid integer", param.Name),
1079 Reason: fmt.Sprintf("The path parameter (which is an array) '%s' is defined as being an integer, "+
1080 "however the value '%s' is not a valid integer", param.Name, item),
1081 SpecLine: specLine,
1082 SpecCol: specCol,
1083 ParameterName: param.Name,
1084 Context: itemsSchema,
1085 HowToFix: fmt.Sprintf(HowToFixParamInvalidNumber, item),
1086 SchemaValidationErrors: []*SchemaValidationFailure{{
1087 Reason: fmt.Sprintf("Array item '%s' is not a valid integer", item),
1088 FieldName: param.Name,
1089 InstancePath: []string{param.Name, "[item]"},
1090 KeywordLocation: keywordLocation,
1091 ReferenceSchema: renderedSchema,
1092 }},
1093 }
1094 }
1095
1096 func IncorrectPathParamArrayBoolean(
1097 param *v3.Parameter, item string, sch *base.Schema, itemsSchema *base.Schema, pathTemplate string, renderedSchema string,
1098 ) *ValidationError {
1099 escapedPath := strings.ReplaceAll(pathTemplate, "~", "~0")
1100 escapedPath = strings.ReplaceAll(escapedPath, "/", "~1")
1101 keywordLocation := fmt.Sprintf("/paths/%s/parameters/%s/schema/items/type", escapedPath, param.Name)
1102 specLine, specCol := schemaItemsTypeLineCol(sch)
1103
1104 return &ValidationError{
1105 ValidationType: helpers.ParameterValidation,
1106 ValidationSubType: helpers.ParameterValidationPath,
1107 Message: fmt.Sprintf("Path array parameter '%s' is not a valid boolean", param.Name),
1108 Reason: fmt.Sprintf("The path parameter (which is an array) '%s' is defined as being a boolean, "+
1109 "however the value '%s' is not a valid boolean", param.Name, item),
1110 SpecLine: specLine,
1111 SpecCol: specCol,
1112 ParameterName: param.Name,
1113 Context: itemsSchema,
1114 HowToFix: fmt.Sprintf(HowToFixParamInvalidBoolean, item),
1115 SchemaValidationErrors: []*SchemaValidationFailure{{
1116 Reason: fmt.Sprintf("Array item '%s' is not a valid boolean", item),
1117 FieldName: param.Name,
1118 InstancePath: []string{param.Name, "[item]"},
1119 KeywordLocation: keywordLocation,
1120 ReferenceSchema: renderedSchema,
1121 }},
1122 }
1123 }
1124
1125 func PathParameterMissing(param *v3.Parameter, pathTemplate string, actualPath string) *ValidationError {
1126 actualSegments := strings.Split(strings.Trim(actualPath, "/"), "/")
1127
1128 encodedPath := strings.ReplaceAll(pathTemplate, "~", "~0")
1129 encodedPath = strings.ReplaceAll(encodedPath, "/", "~1")
1130 encodedPath = strings.TrimPrefix(encodedPath, "~1")
1131 keywordLoc := fmt.Sprintf("/paths/%s/parameters/%s/required", encodedPath, param.Name)
1132 specLine, specCol := paramRequiredLineCol(param)
1133
1134 return &ValidationError{
1135 ValidationType: helpers.ParameterValidation,
1136 ValidationSubType: helpers.ParameterValidationPath,
1137 Message: fmt.Sprintf("Path parameter '%s' is missing", param.Name),
1138 Reason: fmt.Sprintf("The path parameter '%s' is defined as being required, "+
1139 "however it's missing from the requests", param.Name),
1140 SpecLine: specLine,
1141 SpecCol: specCol,
1142 ParameterName: param.Name,
1143 HowToFix: HowToFixMissingValue,
1144 SchemaValidationErrors: []*SchemaValidationFailure{{
1145 Reason: fmt.Sprintf("Required path parameter '%s' is missing from path '%s'", param.Name, actualPath),
1146 FieldName: param.Name,
1147 FieldPath: "",
1148 InstancePath: actualSegments,
1149 KeywordLocation: keywordLoc,
1150 }},
1151 }
1152 }