Browse Source

Merge pull request #4733 from ollama/jyan/isvalidname

added IsValidNamespace function
Josh 11 tháng trước cách đây
mục cha
commit
f6b622c4b3
2 tập tin đã thay đổi với 31 bổ sung0 xóa
  1. 4 0
      types/model/name.go
  2. 27 0
      types/model/name_test.go

+ 4 - 0
types/model/name.go

@@ -251,6 +251,10 @@ func (n Name) DisplayShortest() string {
 	return sb.String()
 	return sb.String()
 }
 }
 
 
+func IsValidNamespace(namespace string) bool {
+	return isValidPart(kindNamespace, namespace)
+}
+
 // IsValid reports whether all parts of the name are present and valid. The
 // 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.
 // digest is a special case, and is checked for validity only if present.
 func (n Name) IsValid() bool {
 func (n Name) IsValid() bool {

+ 27 - 0
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)
+			}
+		})
+	}
+}