diff --git a/kubeval/formats.go b/kubeval/formats.go new file mode 100644 index 0000000..030aa80 --- /dev/null +++ b/kubeval/formats.go @@ -0,0 +1,24 @@ +package kubeval + +import ( + "github.com/xeipuuv/gojsonschema" +) + +// ValidFormat is a type for quickly forcing +// new formats on the gojsonschema loader +type ValidFormat struct{} + +// IsFormat always returns true and meets the +// gojsonschema.FormatChecker interface +func (f ValidFormat) IsFormat(input interface{}) bool { + return true +} + +func init() { + // Without forcing these types the schema fails to load + // Need to work out proper handling for these types + gojsonschema.FormatCheckers.Add("int64", ValidFormat{}) + gojsonschema.FormatCheckers.Add("byte", ValidFormat{}) + gojsonschema.FormatCheckers.Add("int32", ValidFormat{}) + gojsonschema.FormatCheckers.Add("int-or-string", ValidFormat{}) +} diff --git a/kubeval/kubeval.go b/kubeval/kubeval.go index 81173bb..02e8d8a 100644 --- a/kubeval/kubeval.go +++ b/kubeval/kubeval.go @@ -14,24 +14,14 @@ import ( "github.com/instrumenta/kubeval/log" ) -// ValidFormat is a type for quickly forcing -// new formats on the gojsonschema loader -type ValidFormat struct{} - -// IsFormat always returns true and meets the -// gojsonschema.FormatChecker interface -func (f ValidFormat) IsFormat(input interface{}) bool { - return true -} - // ValidationResult contains the details from // validating a given Kubernetes resource type ValidationResult struct { - FileName string - Kind string - APIVersion string - ValidatedAgainstSchema bool - Errors []gojsonschema.ResultError + FileName string + Kind string + APIVersion string + ValidatedAgainstSchema bool + Errors []gojsonschema.ResultError } func determineSchema(kind, apiVersion string, config *Config) string { @@ -150,13 +140,6 @@ func validateAgainstSchema(body interface{}, resource *ValidationResult, schemaC return handleMissingSchema(fmt.Errorf("Failed initalizing schema %s: see first error", schemaRef), config) } - // Without forcing these types the schema fails to load - // Need to Work out proper handling for these types - gojsonschema.FormatCheckers.Add("int64", ValidFormat{}) - gojsonschema.FormatCheckers.Add("byte", ValidFormat{}) - gojsonschema.FormatCheckers.Add("int32", ValidFormat{}) - gojsonschema.FormatCheckers.Add("int-or-string", ValidFormat{}) - documentLoader := gojsonschema.NewGoLoader(body) results, err := schema.Validate(documentLoader) if err != nil {