From 298c996e54c4d562c25c2e88e42432a68b1661b7 Mon Sep 17 00:00:00 2001 From: Josh Yan Date: Thu, 30 May 2024 16:02:07 -0700 Subject: [PATCH 1/3] added IsValidNamespace function --- types/model/name.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/types/model/name.go b/types/model/name.go index f32b2596..2739b53c 100644 --- a/types/model/name.go +++ b/types/model/name.go @@ -251,6 +251,16 @@ func (n Name) DisplayShortest() string { return sb.String() } +func IsValidNamespace(namespace string) bool { + name := Name{ + Host: "h", + Model: "m", + Namespace: namespace, + Tag: "t", + } + return name.IsValid() +} + // IsValid reports whether all parts of the name are present and valid. The // digest is a special case, and is checked for validity only if present. func (n Name) IsValid() bool { From c365f195a88cdc5c121878e0dde40274ac71a78e Mon Sep 17 00:00:00 2001 From: Josh Yan Date: Thu, 30 May 2024 16:40:04 -0700 Subject: [PATCH 2/3] directly use isvalidpart --- types/model/name.go | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/types/model/name.go b/types/model/name.go index 2739b53c..d85fd0c6 100644 --- a/types/model/name.go +++ b/types/model/name.go @@ -252,13 +252,7 @@ func (n Name) DisplayShortest() string { } func IsValidNamespace(namespace string) bool { - name := Name{ - Host: "h", - Model: "m", - Namespace: namespace, - Tag: "t", - } - return name.IsValid() + return isValidPart(kindNamespace, namespace) } // IsValid reports whether all parts of the name are present and valid. The From 2e4da8eec2d19d671df5f58c81dfdfa35de8679c Mon Sep 17 00:00:00 2001 From: Josh Yan Date: Fri, 31 May 2024 11:48:07 -0700 Subject: [PATCH 3/3] added tests for IsValidNamespace --- types/model/name_test.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/types/model/name_test.go b/types/model/name_test.go index 27a8ccf8..26d70ef3 100644 --- a/types/model/name_test.go +++ b/types/model/name_test.go @@ -385,3 +385,30 @@ func FuzzName(f *testing.F) { }) } + +func TestIsValidNamespace(t *testing.T) { + cases := []struct { + username string + expected bool + }{ + {"", false}, + {"a", true}, + {"a:b", false}, + {"a/b", false}, + {"a:b/c", false}, + {"a/b:c", false}, + {"a/b:c", false}, + {"a/b:c/d", false}, + {"a/b:c/d@e", false}, + {"a/b:c/d@sha256-100", false}, + {"himynameisjoe", true}, + {"himynameisreallyreallyreallyreallylongbutitshouldstillbevalid", true}, + } + for _, tt := range cases { + t.Run(tt.username, func(t *testing.T) { + if got := IsValidNamespace(tt.username); got != tt.expected { + t.Errorf("IsValidName(%q) = %v; want %v", tt.username, got, tt.expected) + } + }) + } +}