openapi-validation: Handle List special case

This commit is contained in:
Antoine Pelisse 2017-08-16 15:16:24 -07:00
parent 4e87d2d572
commit a950afecdc
2 changed files with 57 additions and 7 deletions

View File

@ -19,6 +19,7 @@ package validation
import (
"errors"
"fmt"
"strings"
"k8s.io/apimachinery/pkg/runtime/schema"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
@ -49,21 +50,42 @@ func (v *SchemaValidation) ValidateBytes(data []byte) error {
return err
}
if strings.HasSuffix(gvk.Kind, "List") {
return utilerrors.NewAggregate(v.validateList(obj))
}
return utilerrors.NewAggregate(v.validateResource(obj, gvk))
}
func (v *SchemaValidation) validateList(object interface{}) []error {
fields := object.(map[string]interface{})
if fields == nil {
return []error{errors.New("invalid object to validate")}
}
errs := []error{}
for _, item := range fields["items"].([]interface{}) {
if gvk, err := getObjectKind(item); err != nil {
errs = append(errs, err)
} else {
errs = append(errs, v.validateResource(item, gvk)...)
}
}
return errs
}
func (v *SchemaValidation) validateResource(obj interface{}, gvk schema.GroupVersionKind) []error {
resource := v.resources.LookupResource(gvk)
if resource == nil {
return fmt.Errorf("unknown object type %#v", gvk)
return []error{fmt.Errorf("unknown object type %#v", gvk)}
}
rootValidation, err := itemFactory(openapi.NewPath(gvk.Kind), obj)
if err != nil {
return err
return []error{err}
}
resource.Accept(rootValidation)
errs := rootValidation.Errors()
if errs != nil {
return utilerrors.NewAggregate(errs)
}
return nil
return rootValidation.Errors()
}
func parse(data []byte) (interface{}, error) {

View File

@ -328,4 +328,32 @@ spec:
Expect(err).To(BeNil())
})
It("can validate lists", func() {
err := validator.ValidateBytes([]byte(`
apiVersion: v1
kind: List
items:
- apiVersion: v1
kind: Pod
metadata:
labels:
name: redis-master
name: name
spec:
containers:
- name: name
`))
Expect(err).To(Equal(utilerrors.NewAggregate([]error{
validation.ValidationError{
Path: "Pod.spec.containers[0]",
Err: validation.MissingRequiredFieldError{
Path: "io.k8s.api.core.v1.Container",
Field: "image",
},
},
})))
})
})