Skip to content

Commit 464a15b

Browse files
committed
Ensuring type safety on validation types.
when validating a document, there may be a report of an invalid JSON type being submitted. This is an unexpected validation error. It’s now caught correctly. Signed-off-by: Dave Shanley <[email protected]>
1 parent d4e2e98 commit 464a15b

File tree

1 file changed

+30
-26
lines changed

1 file changed

+30
-26
lines changed

schema_validation/validate_document.go

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,38 +28,42 @@ func ValidateOpenAPIDocument(doc libopenapi.Document) (bool, []*errors.Validatio
2828

2929
scErrs := jsch.Validate(decodedDocument)
3030

31+
var schemaValidationErrors []*errors.SchemaValidationFailure
32+
3133
if scErrs != nil {
32-
jk := scErrs.(*jsonschema.ValidationError)
3334

34-
// flatten the validationErrors
35-
schFlatErrs := jk.BasicOutput().Errors
36-
var schemaValidationErrors []*errors.SchemaValidationFailure
37-
for q := range schFlatErrs {
38-
er := schFlatErrs[q]
39-
if er.KeywordLocation == "" || strings.HasPrefix(er.Error, "doesn't validate with") {
40-
continue // ignore this error, it's useless tbh, utter noise.
41-
}
42-
if er.Error != "" {
35+
if jk, ok := scErrs.(*jsonschema.ValidationError); ok {
4336

44-
// locate the violated property in the schema
37+
// flatten the validationErrors
38+
schFlatErrs := jk.BasicOutput().Errors
4539

46-
located := LocateSchemaPropertyNodeByJSONPath(info.RootNode.Content[0], er.KeywordLocation)
47-
if located == nil {
48-
// try again with the instance location
49-
located = LocateSchemaPropertyNodeByJSONPath(info.RootNode.Content[0], er.InstanceLocation)
50-
}
51-
violation := &errors.SchemaValidationFailure{
52-
Reason: er.Error,
53-
Location: er.KeywordLocation,
54-
OriginalError: jk,
40+
for q := range schFlatErrs {
41+
er := schFlatErrs[q]
42+
if er.KeywordLocation == "" || strings.HasPrefix(er.Error, "doesn't validate with") {
43+
continue // ignore this error, it's useless tbh, utter noise.
5544
}
56-
// if we have a location within the schema, add it to the error
57-
if located != nil {
58-
// location of the violation within the rendered schema.
59-
violation.Line = located.Line
60-
violation.Column = located.Column
45+
if er.Error != "" {
46+
47+
// locate the violated property in the schema
48+
49+
located := LocateSchemaPropertyNodeByJSONPath(info.RootNode.Content[0], er.KeywordLocation)
50+
if located == nil {
51+
// try again with the instance location
52+
located = LocateSchemaPropertyNodeByJSONPath(info.RootNode.Content[0], er.InstanceLocation)
53+
}
54+
violation := &errors.SchemaValidationFailure{
55+
Reason: er.Error,
56+
Location: er.KeywordLocation,
57+
OriginalError: jk,
58+
}
59+
// if we have a location within the schema, add it to the error
60+
if located != nil {
61+
// location of the violation within the rendered schema.
62+
violation.Line = located.Line
63+
violation.Column = located.Column
64+
}
65+
schemaValidationErrors = append(schemaValidationErrors, violation)
6166
}
62-
schemaValidationErrors = append(schemaValidationErrors, violation)
6367
}
6468
}
6569

0 commit comments

Comments
 (0)