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

[TEP-0075] Extract out the validation of object keys #4867

Merged
merged 1 commit into from
Jun 7, 2022
Merged
Show file tree
Hide file tree
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
46 changes: 32 additions & 14 deletions pkg/apis/pipeline/v1beta1/task_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ func ValidateParameterVariables(ctx context.Context, steps []Step, params []Para

errs = errs.Also(validateVariables(ctx, steps, "params", parameterNames))
errs = errs.Also(validateArrayUsage(steps, "params", arrayParameterNames))
errs = errs.Also(validateObjectDefault(objectParamSpecs))
return errs.Also(validateObjectUsage(ctx, steps, objectParamSpecs))
}

Expand Down Expand Up @@ -391,32 +392,49 @@ func validateObjectUsage(ctx context.Context, steps []Step, params []ParamSpec)
objectKeys.Insert(key)
}

if p.Default != nil && p.Default.ObjectVal != nil {
errs = errs.Also(validateObjectKeysInDefault(p.Default.ObjectVal, objectKeys, p.Name))
}

// check if the object's key names are referenced correctly i.e. param.objectParam.key1
errs = errs.Also(validateVariables(ctx, steps, fmt.Sprintf("params\\.%s", p.Name), objectKeys))
}

return errs
}

// validate if object keys defined in properties are all provided in default
func validateObjectKeysInDefault(defaultObject map[string]string, neededObjectKeys sets.String, paramName string) (errs *apis.FieldError) {
neededObjectKeysInSpec := neededObjectKeys.List()
providedObjectKeysInDefault := []string{}
for k := range defaultObject {
providedObjectKeysInDefault = append(providedObjectKeysInDefault, k)
// validateObjectDefault validates the keys of all the object params within a
// slice of ParamSpecs are provided in default iff the default section is provided.
func validateObjectDefault(objectParams []ParamSpec) (errs *apis.FieldError) {
for _, p := range objectParams {
errs = errs.Also(validateObjectKeys(p.Properties, p.Default).ViaField(p.Name))
}
return errs
}

// validateObjectKeys validates if object keys defined in properties are all provided in its value provider iff the provider is not nil.
func validateObjectKeys(properties map[string]PropertySpec, propertiesProvider *ArrayOrString) (errs *apis.FieldError) {
if propertiesProvider == nil || propertiesProvider.ObjectVal == nil {
return nil
}

missingObjectKeys := list.DiffLeft(neededObjectKeysInSpec, providedObjectKeysInDefault)
if len(missingObjectKeys) != 0 {
neededKeys := []string{}
providedKeys := []string{}

// collect all needed keys
for key := range properties {
neededKeys = append(neededKeys, key)
}

// collect all provided keys
for key := range propertiesProvider.ObjectVal {
providedKeys = append(providedKeys, key)
}

missings := list.DiffLeft(neededKeys, providedKeys)
if len(missings) != 0 {
return &apis.FieldError{
Message: fmt.Sprintf("Required key(s) %s for the parameter %s are not provided in default.", missingObjectKeys, paramName),
Paths: []string{fmt.Sprintf("%s.properties", paramName), fmt.Sprintf("%s.default", paramName)},
Message: fmt.Sprintf("Required key(s) %s are missing in the value provider.", missings),
Paths: []string{fmt.Sprintf("properties"), fmt.Sprintf("default")},
}
}

return nil
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/pipeline/v1beta1/task_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,7 @@ func TestTaskSpecValidateError(t *testing.T) {
Steps: validSteps,
},
expectedError: apis.FieldError{
Message: fmt.Sprintf("Required key(s) %s for the parameter %s are not provided in default.", []string{"key2"}, "myobjectParam"),
Message: fmt.Sprintf("Required key(s) %s are missing in the value provider.", []string{"key2"}),
Paths: []string{"myobjectParam.properties", "myobjectParam.default"},
},
}, {
Expand Down