Skip to content

Commit 694596f

Browse files
committed
Updated support for most of usescases in #26
bumped `libopenapi` to `v0.12.1` Signed-off-by: Dave Shanley <[email protected]>
1 parent affb03c commit 694596f

File tree

5 files changed

+264
-90
lines changed

5 files changed

+264
-90
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/pb33f/libopenapi-validator
33
go 1.19
44

55
require (
6-
github.com/pb33f/libopenapi v0.10.6
6+
github.com/pb33f/libopenapi v0.12.1
77
github.com/santhosh-tekuri/jsonschema/v5 v5.2.0
88
github.com/stretchr/testify v1.8.0
99
github.com/vmware-labs/yaml-jsonpath v0.3.2

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1y
4848
github.com/onsi/gomega v1.17.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAlGdZY=
4949
github.com/onsi/gomega v1.19.0 h1:4ieX6qQjPP/BfC3mpsAtIGGlxTWPeA3Inl/7DtXw1tw=
5050
github.com/onsi/gomega v1.19.0/go.mod h1:LY+I3pBVzYsTBU1AnDwOSxaYi9WoWiqgwooUqq9yPro=
51-
github.com/pb33f/libopenapi v0.10.6 h1:46iGQqoMm6o5gYK34ER/dpxsSYdh0+76U3+obm5uEyk=
52-
github.com/pb33f/libopenapi v0.10.6/go.mod h1:s8uj6S0DjWrwZVj20ianJBz+MMjHAbeeRYNyo9ird74=
51+
github.com/pb33f/libopenapi v0.12.1 h1:DRhgbg1t32OSYoHT/Bk3stVruqquAkKj+S+OqOQIbBc=
52+
github.com/pb33f/libopenapi v0.12.1/go.mod h1:s8uj6S0DjWrwZVj20ianJBz+MMjHAbeeRYNyo9ird74=
5353
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
5454
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
5555
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=

schema_validation/validate_document.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
package schema_validation
55

66
import (
7+
"errors"
78
"fmt"
89
"github.com/pb33f/libopenapi"
9-
"github.com/pb33f/libopenapi-validator/errors"
10+
liberrors "github.com/pb33f/libopenapi-validator/errors"
1011
"github.com/pb33f/libopenapi-validator/helpers"
1112
"github.com/santhosh-tekuri/jsonschema/v5"
1213
_ "github.com/santhosh-tekuri/jsonschema/v5/httploader"
@@ -16,11 +17,11 @@ import (
1617

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

2122
info := doc.GetSpecInfo()
2223
loadedSchema := info.APISchema
23-
var validationErrors []*errors.ValidationError
24+
var validationErrors []*liberrors.ValidationError
2425
decodedDocument := *info.SpecJSON
2526

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

3031
scErrs := jsch.Validate(decodedDocument)
3132

32-
var schemaValidationErrors []*errors.SchemaValidationFailure
33+
var schemaValidationErrors []*liberrors.SchemaValidationFailure
3334

3435
if scErrs != nil {
3536

36-
if jk, ok := scErrs.(*jsonschema.ValidationError); ok {
37+
var jk *jsonschema.ValidationError
38+
if errors.As(scErrs, &jk) {
3739

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

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

8486
// add the error to the list
85-
validationErrors = append(validationErrors, &errors.ValidationError{
87+
validationErrors = append(validationErrors, &liberrors.ValidationError{
8688
ValidationType: helpers.Schema,
8789
Message: "Document does not pass validation",
8890
Reason: fmt.Sprintf("OpenAPI document is not valid according "+
8991
"to the %s specification", info.Version),
9092
SchemaValidationErrors: schemaValidationErrors,
91-
HowToFix: errors.HowToFixInvalidSchema,
93+
HowToFix: liberrors.HowToFixInvalidSchema,
9294
})
9395
}
9496
if len(validationErrors) > 0 {

0 commit comments

Comments
 (0)