Skip to content

Commit

Permalink
Merge pull request #159 from johanneswuerbach/cache-not-found
Browse files Browse the repository at this point in the history
Cache missing schemas
  • Loading branch information
garethr committed Jul 27, 2019
2 parents 4ab9d7a + ace02d2 commit 82138a4
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions kubeval/kubeval.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,15 @@ func validateAgainstSchema(body interface{}, resource *ValidationResult, schemaC
schemaLoader := gojsonschema.NewReferenceLoader(schemaRef)
var err error
schema, err = gojsonschema.NewSchema(schemaLoader)
schemaCache[schemaRef] = schema

if err != nil {
if IgnoreMissingSchemas {
return []gojsonschema.ResultError{}, nil
}
return []gojsonschema.ResultError{}, fmt.Errorf("Failed initalizing schema %s: %s", schemaRef, err)
return handleMissingSchema(fmt.Errorf("Failed initalizing schema %s: %s", schemaRef, err))
}
schemaCache[schemaRef] = schema
}

if schema == nil {
return handleMissingSchema(fmt.Errorf("Failed initalizing schema %s: see first error", schemaRef))
}

// Without forcing these types the schema fails to load
Expand All @@ -202,6 +204,13 @@ func validateAgainstSchema(body interface{}, resource *ValidationResult, schemaC
return []gojsonschema.ResultError{}, nil
}

func handleMissingSchema(err error) ([]gojsonschema.ResultError, error) {
if IgnoreMissingSchemas {
return []gojsonschema.ResultError{}, nil
}
return []gojsonschema.ResultError{}, err
}

// NewSchemaCache returns a new schema cache to be used with
// ValidateWithCache
func NewSchemaCache() map[string]*gojsonschema.Schema {
Expand Down

0 comments on commit 82138a4

Please sign in to comment.