forked from pb33f/libopenapi-validator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest_errors.go
More file actions
56 lines (49 loc) · 2.06 KB
/
request_errors.go
File metadata and controls
56 lines (49 loc) · 2.06 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
// Copyright 2023 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT
package errors
import (
"fmt"
"net/http"
"strings"
"github.com/pb33f/libopenapi/orderedmap"
v3 "github.com/pb33f/libopenapi/datamodel/high/v3"
"github.com/pb33f/libopenapi-validator/helpers"
)
func RequestContentTypeNotFound(op *v3.Operation, request *http.Request, specPath string) *ValidationError {
ct := request.Header.Get(helpers.ContentTypeHeader)
var ctypes []string
for pair := orderedmap.First(op.RequestBody.Content); pair != nil; pair = pair.Next() {
ctypes = append(ctypes, pair.Key())
}
return &ValidationError{
ValidationType: helpers.RequestBodyValidation,
ValidationSubType: helpers.RequestBodyContentType,
Message: fmt.Sprintf("%s operation request content type '%s' does not exist",
request.Method, ct),
Reason: fmt.Sprintf("The content type '%s' of the %s request submitted has not "+
"been defined, it's an unknown type", ct, request.Method),
SpecLine: op.RequestBody.GoLow().Content.KeyNode.Line,
SpecCol: op.RequestBody.GoLow().Content.KeyNode.Column,
Context: op,
HowToFix: fmt.Sprintf(HowToFixInvalidContentType, orderedmap.Len(op.RequestBody.Content), strings.Join(ctypes, ", ")),
RequestPath: request.URL.Path,
RequestMethod: request.Method,
SpecPath: specPath,
}
}
func OperationNotFound(pathItem *v3.PathItem, request *http.Request, method string, specPath string) *ValidationError {
return &ValidationError{
ValidationType: helpers.RequestValidation,
ValidationSubType: helpers.ValidationMissingOperation,
Message: fmt.Sprintf("%s operation request content type '%s' does not exist",
request.Method, method),
Reason: fmt.Sprintf("The path was found, but there was no '%s' method found in the spec", request.Method),
SpecLine: pathItem.GoLow().KeyNode.Line,
SpecCol: pathItem.GoLow().KeyNode.Column,
Context: pathItem,
HowToFix: HowToFixPathMethod,
RequestPath: request.URL.Path,
RequestMethod: request.Method,
SpecPath: specPath,
}
}