4
4
package schema_validation
5
5
6
6
import (
7
+ "errors"
7
8
"fmt"
8
9
"github.com/pb33f/libopenapi"
9
- "github.com/pb33f/libopenapi-validator/errors"
10
+ liberrors "github.com/pb33f/libopenapi-validator/errors"
10
11
"github.com/pb33f/libopenapi-validator/helpers"
11
12
"github.com/santhosh-tekuri/jsonschema/v5"
12
13
_ "github.com/santhosh-tekuri/jsonschema/v5/httploader"
@@ -16,11 +17,11 @@ import (
16
17
17
18
// ValidateOpenAPIDocument will validate an OpenAPI document against the OpenAPI 2, 3.0 and 3.1 schemas (depending on version)
18
19
// 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 ) {
20
21
21
22
info := doc .GetSpecInfo ()
22
23
loadedSchema := info .APISchema
23
- var validationErrors []* errors .ValidationError
24
+ var validationErrors []* liberrors .ValidationError
24
25
decodedDocument := * info .SpecJSON
25
26
26
27
compiler := jsonschema .NewCompiler ()
@@ -29,11 +30,12 @@ func ValidateOpenAPIDocument(doc libopenapi.Document) (bool, []*errors.Validatio
29
30
30
31
scErrs := jsch .Validate (decodedDocument )
31
32
32
- var schemaValidationErrors []* errors .SchemaValidationFailure
33
+ var schemaValidationErrors []* liberrors .SchemaValidationFailure
33
34
34
35
if scErrs != nil {
35
36
36
- if jk , ok := scErrs .(* jsonschema.ValidationError ); ok {
37
+ var jk * jsonschema.ValidationError
38
+ if errors .As (scErrs , & jk ) {
37
39
38
40
// flatten the validationErrors
39
41
schFlatErrs := jk .BasicOutput ().Errors
@@ -53,7 +55,7 @@ func ValidateOpenAPIDocument(doc libopenapi.Document) (bool, []*errors.Validatio
53
55
54
56
// locate the violated property in the schema
55
57
located := LocateSchemaPropertyNodeByJSONPath (info .RootNode .Content [0 ], er .InstanceLocation )
56
- violation := & errors .SchemaValidationFailure {
58
+ violation := & liberrors .SchemaValidationFailure {
57
59
Reason : er .Error ,
58
60
Location : er .InstanceLocation ,
59
61
DeepLocation : er .KeywordLocation ,
@@ -82,13 +84,13 @@ func ValidateOpenAPIDocument(doc libopenapi.Document) (bool, []*errors.Validatio
82
84
}
83
85
84
86
// add the error to the list
85
- validationErrors = append (validationErrors , & errors .ValidationError {
87
+ validationErrors = append (validationErrors , & liberrors .ValidationError {
86
88
ValidationType : helpers .Schema ,
87
89
Message : "Document does not pass validation" ,
88
90
Reason : fmt .Sprintf ("OpenAPI document is not valid according " +
89
91
"to the %s specification" , info .Version ),
90
92
SchemaValidationErrors : schemaValidationErrors ,
91
- HowToFix : errors .HowToFixInvalidSchema ,
93
+ HowToFix : liberrors .HowToFixInvalidSchema ,
92
94
})
93
95
}
94
96
if len (validationErrors ) > 0 {
0 commit comments