Repository: knative-sandbox/kn-plugin-func
Pull request #1263 (2022-09-20 13:52:59)

fix: validation of function name
# Changes

- :bug: Fixed validation: function name must be DNS 1123 Label.

/kind bug

```release-note
fix: validation of function name -- it must be DNS 1123 Label.
```

resolves: #1259 


knative-prow[bot]: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *<a href="https://github.com/knative-sandbox/kn-plugin-func/pull/1263#" title="Author self-approved">matejvasek</a>*

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=knative-sandbox%2Fkn-plugin-func).

The pull request process is described [here](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process)

<details >
Needs approval from an approver in each of these files:

- ~~[OWNERS](https://github.com/knative-sandbox/kn-plugin-func/blob/main/OWNERS)~~ [matejvasek]

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":[]} -->
codecov[bot]: # [Codecov](https://codecov.io/gh/knative-sandbox/kn-plugin-func/pull/1263?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative-sandbox) Report
> Merging [#1263](https://codecov.io/gh/knative-sandbox/kn-plugin-func/pull/1263?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative-sandbox) (0268a67) into [main](https://codecov.io/gh/knative-sandbox/kn-plugin-func/commit/50243c740a56148ae744f7c20a37e5cd5159bf26?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative-sandbox) (50243c7) will **not change** coverage.
> The diff coverage is `100.00%`.

```diff
@@           Coverage Diff           @@
##             main    #1263   +/-   ##
=======================================
  Coverage   46.40%   46.40%           
=======================================
  Files          71       71           
  Lines       10065    10065           
=======================================
  Hits         4671     4671           
  Misses       4992     4992           
  Partials      402      402           
```


| [Impacted Files](https://codecov.io/gh/knative-sandbox/kn-plugin-func/pull/1263?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative-sandbox) | Coverage Δ | |
|---|---|---|
| [utils/names.go](https://codecov.io/gh/knative-sandbox/kn-plugin-func/pull/1263/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative-sandbox#diff-dXRpbHMvbmFtZXMuZ28=) | `100.00% <100.00%> (ø)` | |

Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative-sandbox). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative-sandbox)


Patch:
diff --git a/utils/names.go b/utils/names.go
index 40114e32..25928c30 100644
--- a/utils/names.go
+++ b/utils/names.go
@@ -23,18 +23,18 @@ type ErrInvalidSecretKey error
 // ErrInvalidLabel indicates the name did not pass label key validation, or the value did not pass label value validation.
 type ErrInvalidLabel error
 
-// ValidateFunctionName validatest that the input name is a valid function name, ie. valid DNS-1123 label.
-// It must consist of lower case alphanumeric characters or '-' and start and end with an alphanumeric character
-// (e.g. 'my-name',  or '123-abc', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?')
+// ValidateFunctionName validates that the input name is a valid function name, ie. valid DNS-1035 label.
+// It must consist of lower case alphanumeric characters or '-' and start with an alphabetic character and end with an alphanumeric character.
+// (e.g. 'my-name',  or 'abc-1', regex used for validation is '[a-z]([-a-z0-9]*[a-z0-9])?')
 func ValidateFunctionName(name string) error {
 
-	if errs := validation.IsDNS1123Label(name); len(errs) > 0 {
+	if errs := validation.IsDNS1035Label(name); len(errs) > 0 {
 		// In case of invalid name the error is this:
-		//	"a lowercase RFC 1123 label must consist of lower case alphanumeric characters or '-',
-		//   and must start and end with an alphanumeric character (e.g. 'my-name',
-		//   or '123-abc', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?')"
-		// Let's reuse it for our purposes, ie. replace "a lowercase RFC 1123 label" substring with "Function name" and the actual function name
-		return ErrInvalidFunctionName(errors.New(strings.Replace(strings.Join(errs, ""), "a lowercase RFC 1123 label", fmt.Sprintf("Function name '%v'", name), 1)))
+		// "a DNS-1035 label must consist of lower case alphanumeric characters or '-',
+		// start with an alphabetic character,
+		// and end with an alphanumeric character".
+		// Let's reuse it for our purposes, ie. replace "a DNS-1035 label" substring with "Function name" and the actual function name
+		return ErrInvalidFunctionName(errors.New(strings.Replace(strings.Join(errs, ""), "a DNS-1035 label", fmt.Sprintf("Function name '%v'", name), 1)))
 	}
 
 	return nil


Test patch:
diff --git a/utils/names_test.go b/utils/names_test.go
index 0e606d5d..e7a257e4 100644
--- a/utils/names_test.go
+++ b/utils/names_test.go
@@ -25,6 +25,7 @@ func TestValidateFunctionName(t *testing.T) {
 		{"example-com-", false},
 		{"Example", false},
 		{"EXAMPLE", false},
+		{"42", false},
 	}
 
 	for _, c := range cases {