Skip to content

Commit c2981af

Browse files
nickajacks1daveshanley
authored andcommitted
refactor(ValidateRequestBody): reduce cyclomatic complexity and indent
1 parent 208c2ec commit c2981af

File tree

1 file changed

+64
-69
lines changed

1 file changed

+64
-69
lines changed

requests/validate_body.go

Lines changed: 64 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -4,94 +4,89 @@
44
package requests
55

66
import (
7+
"net/http"
8+
"strings"
9+
710
"github.com/pb33f/libopenapi-validator/errors"
811
"github.com/pb33f/libopenapi-validator/helpers"
912
"github.com/pb33f/libopenapi-validator/paths"
1013
"github.com/pb33f/libopenapi/datamodel/high/base"
1114
v3 "github.com/pb33f/libopenapi/datamodel/high/v3"
1215
"github.com/pb33f/libopenapi/utils"
13-
"net/http"
14-
"strings"
1516
)
1617

1718
func (v *requestBodyValidator) ValidateRequestBody(request *http.Request) (bool, []*errors.ValidationError) {
1819

1920
// find path
20-
var pathItem *v3.PathItem
21-
var errs []*errors.ValidationError
21+
var pathItem *v3.PathItem = v.pathItem
2222
if v.pathItem == nil {
23-
pathItem, errs, _ = paths.FindPath(request, v.document)
24-
if pathItem == nil || errs != nil {
25-
v.errors = errs
26-
return false, errs
23+
var validationErrors []*errors.ValidationError
24+
pathItem, validationErrors, _ = paths.FindPath(request, v.document)
25+
if pathItem == nil || validationErrors != nil {
26+
v.errors = validationErrors
27+
return false, validationErrors
2728
}
28-
} else {
29-
pathItem = v.pathItem
3029
}
3130

32-
var validationErrors []*errors.ValidationError
3331
operation := helpers.ExtractOperation(request, pathItem)
32+
if operation.RequestBody == nil {
33+
// TODO: check if requestBody is marked as required
34+
return true, nil
35+
}
3436

35-
var contentType string
3637
// extract the content type from the request
38+
contentType := request.Header.Get(helpers.ContentTypeHeader)
39+
if contentType == "" {
40+
//TODO: should this ever return errors?
41+
return true, nil
42+
}
3743

38-
if contentType = request.Header.Get(helpers.ContentTypeHeader); contentType != "" {
39-
40-
// extract the media type from the content type header.
41-
ct, _, _ := helpers.ExtractContentType(contentType)
42-
if operation.RequestBody != nil {
43-
if mediaType, ok := operation.RequestBody.Content[ct]; ok {
44-
45-
// we currently only support JSON validation for request bodies
46-
// this will capture *everything* that contains some form of 'json' in the content type
47-
if strings.Contains(strings.ToLower(contentType), helpers.JSONType) {
48-
49-
// extract schema from media type
50-
if mediaType.Schema != nil {
51-
52-
var schema *base.Schema
53-
var renderedInline, renderedJSON []byte
54-
55-
// have we seen this schema before? let's hash it and check the cache.
56-
hash := mediaType.GoLow().Schema.Value.Hash()
57-
58-
// perform work only once and cache the result in the validator.
59-
if cacheHit, ch := v.schemaCache[hash]; ch {
60-
61-
// got a hit, use cached values
62-
schema = cacheHit.schema
63-
renderedInline = cacheHit.renderedInline
64-
renderedJSON = cacheHit.renderedJSON
65-
66-
} else {
67-
68-
// render the schema inline and perform the intensive work of rendering and converting
69-
// this is only performed once per schema and cached in the validator.
70-
schema = mediaType.Schema.Schema()
71-
renderedInline, _ = schema.RenderInline()
72-
renderedJSON, _ = utils.ConvertYAMLtoJSON(renderedInline)
73-
v.schemaCache[hash] = &schemaCache{
74-
schema: schema,
75-
renderedInline: renderedInline,
76-
renderedJSON: renderedJSON,
77-
}
78-
}
79-
80-
//render the schema, to be used for validation
81-
valid, vErrs := ValidateRequestSchema(request, schema, renderedInline, renderedJSON)
82-
if !valid {
83-
validationErrors = append(validationErrors, vErrs...)
84-
}
85-
}
86-
}
87-
} else {
88-
// content type not found in the contract
89-
validationErrors = append(validationErrors, errors.RequestContentTypeNotFound(operation, request))
90-
}
91-
}
44+
// extract the media type from the content type header.
45+
ct, _, _ := helpers.ExtractContentType(contentType)
46+
mediaType, ok := operation.RequestBody.Content[ct]
47+
if !ok {
48+
return false, []*errors.ValidationError{errors.RequestContentTypeNotFound(operation, request)}
9249
}
93-
if len(validationErrors) > 0 {
94-
return false, validationErrors
50+
51+
// we currently only support JSON validation for request bodies
52+
// this will capture *everything* that contains some form of 'json' in the content type
53+
if !strings.Contains(strings.ToLower(contentType), helpers.JSONType) {
54+
return true, nil
9555
}
96-
return true, nil
56+
57+
// Nothing to validate
58+
if mediaType.Schema == nil {
59+
return true, nil
60+
}
61+
62+
// extract schema from media type
63+
var schema *base.Schema
64+
var renderedInline, renderedJSON []byte
65+
66+
// have we seen this schema before? let's hash it and check the cache.
67+
hash := mediaType.GoLow().Schema.Value.Hash()
68+
69+
// perform work only once and cache the result in the validator.
70+
if cacheHit, ch := v.schemaCache[hash]; ch {
71+
// got a hit, use cached values
72+
schema = cacheHit.schema
73+
renderedInline = cacheHit.renderedInline
74+
renderedJSON = cacheHit.renderedJSON
75+
76+
} else {
77+
78+
// render the schema inline and perform the intensive work of rendering and converting
79+
// this is only performed once per schema and cached in the validator.
80+
schema = mediaType.Schema.Schema()
81+
renderedInline, _ = schema.RenderInline()
82+
renderedJSON, _ = utils.ConvertYAMLtoJSON(renderedInline)
83+
v.schemaCache[hash] = &schemaCache{
84+
schema: schema,
85+
renderedInline: renderedInline,
86+
renderedJSON: renderedJSON,
87+
}
88+
}
89+
90+
//render the schema, to be used for validation
91+
return ValidateRequestSchema(request, schema, renderedInline, renderedJSON)
9792
}

0 commit comments

Comments
 (0)