|
|
|
@ -11,9 +11,28 @@ import (
|
|
|
|
|
|
|
|
|
|
var namePattern = regexp.MustCompile(`^[a-zA-Z][a-zA-Z0-9\.\-_]*$`)
|
|
|
|
|
|
|
|
|
|
type errInvalidName struct {
|
|
|
|
|
error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *errInvalidName) Error() string {
|
|
|
|
|
return e.error.Error()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *errInvalidName) Unwrap() error {
|
|
|
|
|
return e.error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func IsErrInvalidName(err error) bool {
|
|
|
|
|
_, ok := err.(*errInvalidName)
|
|
|
|
|
return ok
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ValidateName(s string) (string, error) {
|
|
|
|
|
if !namePattern.MatchString(s) {
|
|
|
|
|
return "", errors.Errorf("invalid name %s, name needs to start with a letter and may not contain symbols, except ._-", s)
|
|
|
|
|
return "", &errInvalidName{
|
|
|
|
|
errors.Errorf("invalid name %s, name needs to start with a letter and may not contain symbols, except ._-", s),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return strings.ToLower(s), nil
|
|
|
|
|
}
|
|
|
|
|