Skip to content

Commit

Permalink
Check and handle compiler err on compile req body
Browse files Browse the repository at this point in the history
  • Loading branch information
martinsirbe committed Apr 26, 2024
1 parent 688b7a2 commit edbf27a
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions requests/validate_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/pb33f/libopenapi-validator/errors"
"github.com/pb33f/libopenapi-validator/helpers"
"github.com/pb33f/libopenapi-validator/schema_validation"
"github.com/pb33f/libopenapi/datamodel/high/base"
"github.com/santhosh-tekuri/jsonschema/v5"
"gopkg.in/yaml.v3"
"io"
"net/http"
"reflect"
"regexp"
"strconv"
"strings"

"github.com/santhosh-tekuri/jsonschema/v5"
"gopkg.in/yaml.v3"

"github.com/pb33f/libopenapi-validator/errors"
"github.com/pb33f/libopenapi-validator/helpers"
"github.com/pb33f/libopenapi-validator/schema_validation"
"github.com/pb33f/libopenapi/datamodel/high/base"
)

var instanceLocationRegex = regexp.MustCompile(`^/(\d+)`)
Expand Down Expand Up @@ -97,7 +99,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

Check warning on line 111 in requests/validate_request.go

View check run for this annotation

Codecov / codecov/patch

requests/validate_request.go#L104-L111

Added lines #L104 - L111 were not covered by tests
}

// validate the object against the schema
scErrs := jsch.Validate(decodedObj)
Expand Down

0 comments on commit edbf27a

Please sign in to comment.