mirror of
https://github.com/yannh/kubeconform.git
synced 2026-04-10 23:14:17 +00:00
try to validate
This commit is contained in:
parent
fe33d14dc4
commit
96e35cb926
6 changed files with 87 additions and 21 deletions
42
pkg/validator/main.go
Normal file
42
pkg/validator/main.go
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
package validator
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/xeipuuv/gojsonschema"
|
||||
)
|
||||
|
||||
// ValidFormat is a type for quickly forcing
|
||||
// new formats on the gojsonschema loader
|
||||
type ValidFormat struct{}
|
||||
func (f ValidFormat) IsFormat(input interface{}) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func init () {
|
||||
gojsonschema.FormatCheckers.Add("int64", ValidFormat{})
|
||||
gojsonschema.FormatCheckers.Add("byte", ValidFormat{})
|
||||
gojsonschema.FormatCheckers.Add("int32", ValidFormat{})
|
||||
gojsonschema.FormatCheckers.Add("int-or-string", ValidFormat{})
|
||||
}
|
||||
|
||||
func Validate(resource interface{}, rawSchema []byte) error {
|
||||
schemaLoader := gojsonschema.NewBytesLoader(rawSchema)
|
||||
schema, err := gojsonschema.NewSchema(schemaLoader)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
documentLoader := gojsonschema.NewGoLoader(resource)
|
||||
results, err := schema.Validate(documentLoader)
|
||||
if err != nil {
|
||||
// This error can only happen if the Object to validate is poorly formed. There's no hope of saving this one
|
||||
return fmt.Errorf("problem validating schema. Check JSON formatting: %s", err)
|
||||
}
|
||||
|
||||
if !results.Valid() {
|
||||
return fmt.Errorf("resource does not conform to schema")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue