MCPcopy Index your code
hub / github.com/bytebase/bytebase / validateLabels

Function validateLabels

backend/api/v1/common.go:52–71  ·  view source on GitHub ↗

validateLabels validates labels according to the requirements. Labels must follow these rules: - Maximum 64 labels allowed - Keys must start with lowercase letter, then contain only lowercase letters, numbers, underscores, and dashes (max 63 chars) - Values can contain letters, numbers, underscores,

(labels map[string]string)

Source from the content-addressed store, hash-verified

50// - Keys must start with lowercase letter, then contain only lowercase letters, numbers, underscores, and dashes (max 63 chars)
51// - Values can contain letters, numbers, underscores, and dashes (max 63 chars, can be empty)
52func validateLabels(labels map[string]string) error {
53 if len(labels) > 64 {
54 return errors.Errorf("maximum 64 labels allowed, got %d", len(labels))
55 }
56 // Key pattern: must start with lowercase letter, then lowercase letters, numbers, underscores, dashes (max 63 chars)
57 keyPattern := `^[a-z][a-z0-9_-]{0,62}$`
58 // Value pattern: letters, numbers, underscores, dashes (max 63 chars, can be empty)
59 valuePattern := `^[a-zA-Z0-9_-]{0,63}$`
60 keyRegex := regexp.MustCompile(keyPattern)
61 valueRegex := regexp.MustCompile(valuePattern)
62 for key, value := range labels {
63 if !keyRegex.MatchString(key) {
64 return errors.Errorf("invalid label key %q: must start with lowercase letter and contain only lowercase letters, numbers, underscores, and dashes (max 63 chars)", key)
65 }
66 if !valueRegex.MatchString(value) {
67 return errors.Errorf("invalid label value %q for key %q: must contain only letters, numbers, underscores, and dashes (max 63 chars)", value, key)
68 }
69 }
70 return nil
71}
72
73type Expression struct {
74 Key string

Callers 5

CreateProjectMethod · 0.85
UpdateProjectMethod · 0.85
CreateInstanceMethod · 0.85
UpdateInstanceMethod · 0.85
TestValidateLabelsFunction · 0.85

Calls 1

ErrorfMethod · 0.80

Tested by 1

TestValidateLabelsFunction · 0.68