-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
validator.go
34 lines (29 loc) · 935 Bytes
/
validator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package topo
import (
vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
"vitess.io/vitess/go/vt/vterrors"
)
// ValidateObjectName checks that the name is a valid object name.
// Object names are used for things like keyspace and shard names
// and must match specific constraints.
// They are only allowed to use ASCII letters or digits, - and _.
// No spaces or special characters are allowed.
func validateObjectName(name string) error {
if name == "" {
return vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "empty name")
}
if len(name) > 64 {
return vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "name %v is too long", name)
}
for _, c := range name {
switch {
case 'a' <= c && c <= 'z':
case 'A' <= c && c <= 'Z':
case '0' <= c && c <= '9':
case c == '-' || c == '_':
default:
return vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "invalid character %s in name %v", string(c), name)
}
}
return nil
}