Skip to content

Commit

Permalink
Cache missing schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
johanneswuerbach committed Jul 26, 2019
1 parent 70e32d6 commit ace02d2
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions kubeval/kubeval.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,6 @@ func validateResource(data []byte, fileName string, schemaCache map[string]*gojs
return result, nil
}


func validateAgainstSchema(body interface{}, resource *ValidationResult, schemaCache map[string]*gojsonschema.Schema) ([]gojsonschema.ResultError, error) {
if IgnoreMissingSchemas {
log.Warn("Warning: Set to ignore missing schemas")
Expand All @@ -227,13 +226,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 @@ -255,6 +256,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 ace02d2

Please sign in to comment.