Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion requests/validate_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,17 @@ func ValidateRequestSchema(

compiler := jsonschema.NewCompiler()
_ = compiler.AddResource("requestBody.json", strings.NewReader(string(jsonSchema)))
jsch, _ := compiler.Compile("requestBody.json")
jsch, err := compiler.Compile("requestBody.json")
if err != nil {
validationErrors = append(validationErrors, &errors.ValidationError{
ValidationType: helpers.RequestBodyValidation,
ValidationSubType: helpers.Schema,
Message: err.Error(),
Reason: "Failed to compile the request body for validation.",
Context: string(renderedSchema),
})
return false, validationErrors
}

// validate the object against the schema
scErrs := jsch.Validate(decodedObj)
Expand Down
86 changes: 86 additions & 0 deletions requests/validate_request_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package requests

import (
"io"
"net/http"
"strings"
"testing"

"github.com/pb33f/libopenapi/datamodel/high/base"
"github.com/stretchr/testify/assert"
)

func TestValidateRequestSchema(t *testing.T) {
for name, tc := range map[string]struct {
request *http.Request
schema *base.Schema
renderedSchema, jsonSchema []byte
assertValidRequestSchema assert.BoolAssertionFunc
expectedErrorsCount int
}{
"FailOnBooleanExclusiveMinimum": {
request: postRequestWithBody(`{"exclusiveNumber": 13}`),
schema: &base.Schema{
Type: []string{"object"},
},
renderedSchema: []byte(`type: object
properties:
exclusiveNumber:
type: number
description: This number starts its journey where most numbers are too scared to begin!
exclusiveMinimum: true
minimum: !!float 10`),
jsonSchema: []byte(`{"properties":{"exclusiveNumber":{"description":"This number starts its journey where most numbers are too scared to begin!","exclusiveMinimum":true,"minimum":10,"type":"number"}},"type":"object"}`),
assertValidRequestSchema: assert.False,
expectedErrorsCount: 1,
},
"PassWithCorrectExclusiveMinimum": {
request: postRequestWithBody(`{"exclusiveNumber": 15}`),
schema: &base.Schema{
Type: []string{"object"},
},
renderedSchema: []byte(`type: object
properties:
exclusiveNumber:
type: number
description: This number is properly constrained by a numeric exclusive minimum.
exclusiveMinimum: 12
minimum: 12`),
jsonSchema: []byte(`{"properties":{"exclusiveNumber":{"type":"number","description":"This number is properly constrained by a numeric exclusive minimum.","exclusiveMinimum":12,"minimum":12}},"type":"object"}`),
assertValidRequestSchema: assert.True,
expectedErrorsCount: 0,
},
"PassWithValidStringType": {
request: postRequestWithBody(`{"greeting": "Hello, world!"}`),
schema: &base.Schema{
Type: []string{"object"},
},
renderedSchema: []byte(`type: object
properties:
greeting:
type: string
description: A simple greeting
example: "Hello, world!"`),
jsonSchema: []byte(`{"properties":{"greeting":{"type":"string","description":"A simple greeting","example":"Hello, world!"}},"type":"object"}`),
assertValidRequestSchema: assert.True,
expectedErrorsCount: 0,
},
} {
tc := tc
t.Run(name, func(t *testing.T) {
t.Parallel()

valid, errors := ValidateRequestSchema(tc.request, tc.schema, tc.renderedSchema, tc.jsonSchema)

tc.assertValidRequestSchema(t, valid)
assert.Len(t, errors, tc.expectedErrorsCount)
})
}
}

func postRequestWithBody(payload string) *http.Request {
return &http.Request{
Method: http.MethodPost,
Body: io.NopCloser(strings.NewReader(payload)),
}
}