Skip to content

Commit

Permalink
Updated support for most of usescases in #26
Browse files Browse the repository at this point in the history
bumped `libopenapi` to `v0.12.1`

Signed-off-by: Dave Shanley <[email protected]>
  • Loading branch information
daveshanley committed Oct 8, 2023
1 parent affb03c commit 694596f
Show file tree
Hide file tree
Showing 5 changed files with 264 additions and 90 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/pb33f/libopenapi-validator
go 1.19

require (
github.com/pb33f/libopenapi v0.10.6
github.com/pb33f/libopenapi v0.12.1
github.com/santhosh-tekuri/jsonschema/v5 v5.2.0
github.com/stretchr/testify v1.8.0
github.com/vmware-labs/yaml-jsonpath v0.3.2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1y
github.com/onsi/gomega v1.17.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAlGdZY=
github.com/onsi/gomega v1.19.0 h1:4ieX6qQjPP/BfC3mpsAtIGGlxTWPeA3Inl/7DtXw1tw=
github.com/onsi/gomega v1.19.0/go.mod h1:LY+I3pBVzYsTBU1AnDwOSxaYi9WoWiqgwooUqq9yPro=
github.com/pb33f/libopenapi v0.10.6 h1:46iGQqoMm6o5gYK34ER/dpxsSYdh0+76U3+obm5uEyk=
github.com/pb33f/libopenapi v0.10.6/go.mod h1:s8uj6S0DjWrwZVj20ianJBz+MMjHAbeeRYNyo9ird74=
github.com/pb33f/libopenapi v0.12.1 h1:DRhgbg1t32OSYoHT/Bk3stVruqquAkKj+S+OqOQIbBc=
github.com/pb33f/libopenapi v0.12.1/go.mod h1:s8uj6S0DjWrwZVj20ianJBz+MMjHAbeeRYNyo9ird74=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand Down
18 changes: 10 additions & 8 deletions schema_validation/validate_document.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
package schema_validation

import (
"errors"
"fmt"
"github.com/pb33f/libopenapi"
"github.com/pb33f/libopenapi-validator/errors"
liberrors "github.com/pb33f/libopenapi-validator/errors"
"github.com/pb33f/libopenapi-validator/helpers"
"github.com/santhosh-tekuri/jsonschema/v5"
_ "github.com/santhosh-tekuri/jsonschema/v5/httploader"
Expand All @@ -16,11 +17,11 @@ import (

// ValidateOpenAPIDocument will validate an OpenAPI document against the OpenAPI 2, 3.0 and 3.1 schemas (depending on version)
// It will return true if the document is valid, false if it is not and a slice of ValidationError pointers.
func ValidateOpenAPIDocument(doc libopenapi.Document) (bool, []*errors.ValidationError) {
func ValidateOpenAPIDocument(doc libopenapi.Document) (bool, []*liberrors.ValidationError) {

info := doc.GetSpecInfo()
loadedSchema := info.APISchema
var validationErrors []*errors.ValidationError
var validationErrors []*liberrors.ValidationError
decodedDocument := *info.SpecJSON

compiler := jsonschema.NewCompiler()
Expand All @@ -29,11 +30,12 @@ func ValidateOpenAPIDocument(doc libopenapi.Document) (bool, []*errors.Validatio

scErrs := jsch.Validate(decodedDocument)

var schemaValidationErrors []*errors.SchemaValidationFailure
var schemaValidationErrors []*liberrors.SchemaValidationFailure

if scErrs != nil {

if jk, ok := scErrs.(*jsonschema.ValidationError); ok {
var jk *jsonschema.ValidationError
if errors.As(scErrs, &jk) {

// flatten the validationErrors
schFlatErrs := jk.BasicOutput().Errors
Expand All @@ -53,7 +55,7 @@ func ValidateOpenAPIDocument(doc libopenapi.Document) (bool, []*errors.Validatio

// locate the violated property in the schema
located := LocateSchemaPropertyNodeByJSONPath(info.RootNode.Content[0], er.InstanceLocation)
violation := &errors.SchemaValidationFailure{
violation := &liberrors.SchemaValidationFailure{
Reason: er.Error,
Location: er.InstanceLocation,
DeepLocation: er.KeywordLocation,
Expand Down Expand Up @@ -82,13 +84,13 @@ func ValidateOpenAPIDocument(doc libopenapi.Document) (bool, []*errors.Validatio
}

// add the error to the list
validationErrors = append(validationErrors, &errors.ValidationError{
validationErrors = append(validationErrors, &liberrors.ValidationError{
ValidationType: helpers.Schema,
Message: "Document does not pass validation",
Reason: fmt.Sprintf("OpenAPI document is not valid according "+
"to the %s specification", info.Version),
SchemaValidationErrors: schemaValidationErrors,
HowToFix: errors.HowToFixInvalidSchema,
HowToFix: liberrors.HowToFixInvalidSchema,
})
}
if len(validationErrors) > 0 {
Expand Down
Loading

0 comments on commit 694596f

Please sign in to comment.