Skip to content

Commit 8660f37

Browse files
reversearrowdaveshanley
authored andcommitted
fix: normalize the JSON spec before passing to the JSON Schema validator
Ensures only JSON-compatible types are passed to the JSON Schema validator.
1 parent 55e1e24 commit 8660f37

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

schema_validation/validate_document.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package schema_validation
55

66
import (
7+
"encoding/json"
78
"errors"
89
"fmt"
910

@@ -18,6 +19,13 @@ import (
1819
"github.com/pb33f/libopenapi-validator/helpers"
1920
)
2021

22+
func normalizeJSON(data any) any {
23+
d, _ := json.Marshal(data)
24+
var normalized any
25+
_ = json.Unmarshal(d, &normalized)
26+
return normalized
27+
}
28+
2129
// ValidateOpenAPIDocument will validate an OpenAPI document against the OpenAPI 2, 3.0 and 3.1 schemas (depending on version)
2230
// It will return true if the document is valid, false if it is not and a slice of ValidationError pointers.
2331
func ValidateOpenAPIDocument(doc libopenapi.Document, opts ...config.Option) (bool, []*liberrors.ValidationError) {
@@ -32,7 +40,7 @@ func ValidateOpenAPIDocument(doc libopenapi.Document, opts ...config.Option) (bo
3240
jsch, _ := helpers.NewCompiledSchema("schema", []byte(loadedSchema), options)
3341

3442
// Validate the document
35-
scErrs := jsch.Validate(decodedDocument)
43+
scErrs := jsch.Validate(normalizeJSON(decodedDocument))
3644

3745
var schemaValidationErrors []*liberrors.SchemaValidationFailure
3846

0 commit comments

Comments
 (0)