diff --git a/pkg/engine/validate/common.go b/pkg/engine/validate/common.go index 9009da7c03..e9185f742c 100644 --- a/pkg/engine/validate/common.go +++ b/pkg/engine/validate/common.go @@ -5,8 +5,12 @@ import ( "strconv" ) -// convertToString converts value to string -func convertToString(value interface{}) (string, error) { +// convertNumberToString converts value to string +func convertNumberToString(value interface{}) (string, error) { + if value == nil { + return "0", nil + } + switch typed := value.(type) { case string: return string(typed), nil @@ -17,7 +21,7 @@ func convertToString(value interface{}) (string, error) { case int: return strconv.Itoa(typed), nil default: - return "", fmt.Errorf("Could not convert %T to string", value) + return "", fmt.Errorf("could not convert %v to string", typed) } } diff --git a/pkg/engine/validate/pattern.go b/pkg/engine/validate/pattern.go index 7e2edf23ae..7190ebff62 100644 --- a/pkg/engine/validate/pattern.go +++ b/pkg/engine/validate/pattern.go @@ -224,7 +224,7 @@ func validateString(log logr.Logger, value interface{}, pattern string, operator // validateNumberWithStr compares quantity if pattern type is quantity // or a wildcard match to pattern string func validateNumberWithStr(log logr.Logger, value interface{}, pattern string, operator operator.Operator) bool { - typedValue, err := convertToString(value) + typedValue, err := convertNumberToString(value) if err != nil { log.Error(err, "failed to convert to string") return false