diff --git a/db.go b/db.go index 6ad81e16b..9348c97b6 100644 --- a/db.go +++ b/db.go @@ -49,9 +49,9 @@ type DB struct { // NewDB returns a new instance of DB. func NewDB(path, name string) (*DB, error) { - validName := Exp.FindStringSubmatchIndex(name) - if len(validName) == 0 { - return nil, ErrName + err := ValidateName(name) + if err != nil { + return nil, err } return &DB{ diff --git a/frame.go b/frame.go index 368cde821..fa47517c5 100644 --- a/frame.go +++ b/frame.go @@ -47,9 +47,9 @@ type Frame struct { // NewFrame returns a new instance of frame. func NewFrame(path, db, name string) (*Frame, error) { - validName := Exp.FindStringSubmatchIndex(name) - if len(validName) == 0 { - return nil, ErrName + err := ValidateName(name) + if err != nil { + return nil, err } return &Frame{ diff --git a/pilosa.go b/pilosa.go index b3cee7450..b3620a900 100644 --- a/pilosa.go +++ b/pilosa.go @@ -28,7 +28,8 @@ var ( ) // Regular expression to valuate db and frame's name -var Exp = regexp.MustCompile(`^([a-z0-9._-]{1,64}$)`) +// Todo: remove . when frame doesn't require . for topN +var nameRegexp = regexp.MustCompile(`^([a-z0-9._-]{1,64}$)`) // Profile represents vertical column in a database. // A profile can have a set of attributes attached to it. @@ -82,3 +83,13 @@ func decodeProfile(pb *internal.Profile) *Profile { // TimeFormat is the go-style time format used to parse string dates. const TimeFormat = "2006-01-02T15:04" + + +// Restrict name using regex +func ValidateName(name string) error { + validName := nameRegexp.Match([]byte(name)) + if validName == false{ + return ErrName + } + return nil +} \ No newline at end of file