Commit 0c0a4696 authored by Sahdev P. Zala's avatar Sahdev P. Zala

Provide aggregated validation errors for version and kind

Currently the validation checks is done individually for version and kind group. For example, if user provided yaml file is missing apiVersion and kind fields, first they will receive error on apiVersion. Once user update the file and try to recreate, an error on missing kind is displayed. The behavior is same for wrong types of the fields. These errors should be aggregated and displayed.
parent 8b1bdd8e
...@@ -43,9 +43,9 @@ func (v *SchemaValidation) ValidateBytes(data []byte) error { ...@@ -43,9 +43,9 @@ func (v *SchemaValidation) ValidateBytes(data []byte) error {
return err return err
} }
gvk, err := getObjectKind(obj) gvk, errs := getObjectKind(obj)
if err != nil { if errs != nil {
return err return utilerrors.NewAggregate(errs)
} }
if strings.HasSuffix(gvk.Kind, "List") { if strings.HasSuffix(gvk.Kind, "List") {
...@@ -61,15 +61,15 @@ func (v *SchemaValidation) validateList(object interface{}) []error { ...@@ -61,15 +61,15 @@ func (v *SchemaValidation) validateList(object interface{}) []error {
return []error{errors.New("invalid object to validate")} return []error{errors.New("invalid object to validate")}
} }
errs := []error{} allErrors := []error{}
for _, item := range fields["items"].([]interface{}) { for _, item := range fields["items"].([]interface{}) {
if gvk, err := getObjectKind(item); err != nil { if gvk, errs := getObjectKind(item); errs != nil {
errs = append(errs, err) allErrors = append(allErrors, errs...)
} else { } else {
errs = append(errs, v.validateResource(item, gvk)...) allErrors = append(allErrors, v.validateResource(item, gvk)...)
} }
} }
return errs return allErrors
} }
func (v *SchemaValidation) validateResource(obj interface{}, gvk schema.GroupVersionKind) []error { func (v *SchemaValidation) validateResource(obj interface{}, gvk schema.GroupVersionKind) []error {
...@@ -99,29 +99,39 @@ func parse(data []byte) (interface{}, error) { ...@@ -99,29 +99,39 @@ func parse(data []byte) (interface{}, error) {
return obj, nil return obj, nil
} }
func getObjectKind(object interface{}) (schema.GroupVersionKind, error) { func getObjectKind(object interface{}) (schema.GroupVersionKind, []error) {
var listErrors []error
fields := object.(map[string]interface{}) fields := object.(map[string]interface{})
if fields == nil { if fields == nil {
return schema.GroupVersionKind{}, errors.New("invalid object to validate") listErrors = append(listErrors, errors.New("invalid object to validate"))
return schema.GroupVersionKind{}, listErrors
} }
var group string
var version string
apiVersion := fields["apiVersion"] apiVersion := fields["apiVersion"]
if apiVersion == nil { if apiVersion == nil {
return schema.GroupVersionKind{}, errors.New("apiVersion not set") listErrors = append(listErrors, errors.New("apiVersion not set"))
} } else if _, ok := apiVersion.(string); !ok {
if _, ok := apiVersion.(string); !ok { listErrors = append(listErrors, errors.New("apiVersion isn't string type"))
return schema.GroupVersionKind{}, errors.New("apiVersion isn't string type") } else {
} gv, err := schema.ParseGroupVersion(apiVersion.(string))
gv, err := schema.ParseGroupVersion(apiVersion.(string)) if err != nil {
if err != nil { listErrors = append(listErrors, err)
return schema.GroupVersionKind{}, err } else {
group = gv.Group
version = gv.Version
}
} }
kind := fields["kind"] kind := fields["kind"]
if kind == nil { if kind == nil {
return schema.GroupVersionKind{}, errors.New("kind not set") listErrors = append(listErrors, errors.New("kind not set"))
} else if _, ok := kind.(string); !ok {
listErrors = append(listErrors, errors.New("kind isn't string type"))
} }
if _, ok := kind.(string); !ok { if listErrors != nil {
return schema.GroupVersionKind{}, errors.New("kind isn't string type") return schema.GroupVersionKind{}, listErrors
} }
return schema.GroupVersionKind{Group: gv.Group, Version: gv.Version, Kind: kind.(string)}, nil return schema.GroupVersionKind{Group: group, Version: version, Kind: kind.(string)}, nil
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment