Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache missing schemas #159

Merged
merged 1 commit into from
Jul 27, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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