validation: make uuid like names not valid

names that are valid uuids are not valid. This is needed to accept both names or
uuid in rest APIs without using a special syntax to distinguish them
This commit is contained in:
Simone Gotti 2019-05-02 23:40:28 +02:00
parent bad18bf814
commit 0471bf0c30
2 changed files with 15 additions and 0 deletions

View File

@ -17,6 +17,8 @@ package util
import ( import (
"errors" "errors"
"regexp" "regexp"
uuid "github.com/satori/go.uuid"
) )
var nameRegexp = regexp.MustCompile(`^[a-zA-Z][a-zA-Z0-9]*([-]?[a-zA-Z0-9]+)+$`) var nameRegexp = regexp.MustCompile(`^[a-zA-Z][a-zA-Z0-9]*([-]?[a-zA-Z0-9]+)+$`)
@ -26,5 +28,10 @@ var (
) )
func ValidateName(s string) bool { func ValidateName(s string) bool {
// names that are valid uuids are not valid. This is needed to accept both
// names or uuid in rest APIs
if _, err := uuid.FromString(s); err == nil {
return false
}
return nameRegexp.MatchString(s) return nameRegexp.MatchString(s)
} }

View File

@ -25,6 +25,10 @@ var (
"foo-1", "foo-1",
"foo-1-bar", "foo-1-bar",
"f12oo-bar33", "f12oo-bar33",
"cba7b810-9dad-11d1-80b4-00c04fd430c",
"cba7b810-9dad-11d1-80b4000c04fd430c8",
"cba7b8109dad11d180b400c04fd430c89",
"cba7b8109dad11d180b400c04fd430c",
} }
badNames = []string{ badNames = []string{
"", "",
@ -40,6 +44,10 @@ var (
"foo_bar", "foo_bar",
"foo#bar", "foo#bar",
"1foobar", "1foobar",
"cba7b810-9dad-11d1-80b4-00c04fd430c8",
"{cba7b810-9dad-11d1-80b4-00c04fd430c8}",
"urn:uuid:cba7b810-9dad-11d1-80b4-00c04fd430c8",
"cba7b8109dad11d180b400c04fd430c8",
} }
) )