Skip to content

Commit

Permalink
Ensuring type safety on validation types.
Browse files Browse the repository at this point in the history
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 <dave@quobix.com>
  • Loading branch information
daveshanley committed May 17, 2023
1 parent d4e2e98 commit d9b4c47
Showing 1 changed file with 30 additions and 26 deletions.
56 changes: 30 additions & 26 deletions schema_validation/validate_document.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,38 +28,42 @@ func ValidateOpenAPIDocument(doc libopenapi.Document) (bool, []*errors.Validatio

scErrs := jsch.Validate(decodedDocument)

var schemaValidationErrors []*errors.SchemaValidationFailure

if scErrs != nil {
jk := scErrs.(*jsonschema.ValidationError)

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

// locate the violated property in the schema
// flatten the validationErrors
schFlatErrs := jk.BasicOutput().Errors

located := LocateSchemaPropertyNodeByJSONPath(info.RootNode.Content[0], er.KeywordLocation)
if located == nil {
// try again with the instance location
located = LocateSchemaPropertyNodeByJSONPath(info.RootNode.Content[0], er.InstanceLocation)
}
violation := &errors.SchemaValidationFailure{
Reason: er.Error,
Location: er.KeywordLocation,
OriginalError: jk,
for q := range schFlatErrs {
er := schFlatErrs[q]
if er.KeywordLocation == "" || strings.HasPrefix(er.Error, "doesn't validate with") {
continue // ignore this error, it's useless tbh, utter noise.
}
// if we have a location within the schema, add it to the error
if located != nil {
// location of the violation within the rendered schema.
violation.Line = located.Line
violation.Column = located.Column
if er.Error != "" {

// locate the violated property in the schema

located := LocateSchemaPropertyNodeByJSONPath(info.RootNode.Content[0], er.KeywordLocation)
if located == nil {
// try again with the instance location
located = LocateSchemaPropertyNodeByJSONPath(info.RootNode.Content[0], er.InstanceLocation)
}
violation := &errors.SchemaValidationFailure{
Reason: er.Error,
Location: er.KeywordLocation,
OriginalError: jk,
}
// if we have a location within the schema, add it to the error
if located != nil {
// location of the violation within the rendered schema.
violation.Line = located.Line
violation.Column = located.Column
}
schemaValidationErrors = append(schemaValidationErrors, violation)
}
schemaValidationErrors = append(schemaValidationErrors, violation)
}
}

Expand Down

0 comments on commit d9b4c47

Please sign in to comment.