mirror of
https://github.com/featurebasedb/featurebase.git
synced 2026-09-13 16:11:01 +00:00
add labelRegexp to validate row and column labels
This commit is contained in:
parent
77feba689d
commit
692d997cab
4 changed files with 64 additions and 7 deletions
2
frame.go
2
frame.go
|
|
@ -140,7 +140,7 @@ func (f *Frame) SetRowLabel(v string) error {
|
|||
}
|
||||
|
||||
// Make sure rowLabel is valid name
|
||||
err := ValidateName(v)
|
||||
err := ValidateLabel(v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -100,20 +100,66 @@ func TestFrame_NameValidation(t *testing.T) {
|
|||
"a12345678901234567890123456789012345678901234567890123456789012345",
|
||||
}
|
||||
|
||||
path, err := ioutil.TempDir("", "pilosa-frame-")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
for _, name := range validFrameNames {
|
||||
_, err := pilosa.NewFrame("", "i", name)
|
||||
_, err := pilosa.NewFrame(path, "i", name)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected frame name: %s %s", name, err)
|
||||
}
|
||||
}
|
||||
for _, name := range invalidFrameNames {
|
||||
_, err := pilosa.NewFrame("", "i", name)
|
||||
_, err := pilosa.NewFrame(path, "i", name)
|
||||
if err == nil {
|
||||
t.Fatalf("expected error on frame name: %s", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Ensure that frame RowLable validation is consistent.
|
||||
func TestFrame_RowLabelValidation(t *testing.T) {
|
||||
validRowLabels := []string{
|
||||
"",
|
||||
"foo",
|
||||
"hyphen-ated",
|
||||
"under_score",
|
||||
"abc123",
|
||||
"trailing_",
|
||||
"camelCase",
|
||||
"UPPERCASE",
|
||||
}
|
||||
invalidRowLabels := []string{
|
||||
"x.y",
|
||||
"_foo",
|
||||
"-bar",
|
||||
"abc def",
|
||||
"a12345678901234567890123456789012345678901234567890123456789012345",
|
||||
}
|
||||
|
||||
path, err := ioutil.TempDir("", "pilosa-frame-")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
f, err := pilosa.NewFrame(path, "i", "f")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected frame error: %s", err)
|
||||
}
|
||||
|
||||
for _, label := range validRowLabels {
|
||||
if err := f.SetRowLabel(label); err != nil {
|
||||
t.Fatalf("unexpected row label: %s %s", label, err)
|
||||
}
|
||||
}
|
||||
for _, label := range invalidRowLabels {
|
||||
if err := f.SetRowLabel(label); err == nil {
|
||||
t.Fatalf("expected error on row label: %s", label)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Frame represents a test wrapper for pilosa.Frame.
|
||||
type Frame struct {
|
||||
*pilosa.Frame
|
||||
|
|
|
|||
2
index.go
2
index.go
|
|
@ -108,7 +108,7 @@ func (i *Index) SetColumnLabel(v string) error {
|
|||
}
|
||||
|
||||
// Make sure columnLabel is valid name
|
||||
err := ValidateName(v)
|
||||
err := ValidateLabel(v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
17
pilosa.go
17
pilosa.go
|
|
@ -38,7 +38,8 @@ var (
|
|||
ErrInvalidView = errors.New("invalid view")
|
||||
ErrInvalidCacheType = errors.New("invalid cache type")
|
||||
|
||||
ErrName = errors.New("invalid index or frame's name, must match [a-z0-9_-]")
|
||||
ErrName = errors.New("invalid index or frame's name, must match [a-z0-9_-]")
|
||||
ErrLabel = errors.New("invalid row or column label, must match [A-Za-z0-9_-]")
|
||||
|
||||
// ErrFragmentNotFound is returned when a fragment does not exist.
|
||||
ErrFragmentNotFound = errors.New("fragment not found")
|
||||
|
|
@ -48,6 +49,9 @@ var (
|
|||
// Regular expression to validate index and frame names.
|
||||
var nameRegexp = regexp.MustCompile(`^[a-z0-9][a-z0-9_-]{0,64}$`)
|
||||
|
||||
// Regular expression to validate row and column labels.
|
||||
var labelRegexp = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9_-]{0,64}$`)
|
||||
|
||||
// ColumnAttrSet represents a set of attributes for a vertical column in an index.
|
||||
// Can have a set of attributes attached to it.
|
||||
type ColumnAttrSet struct {
|
||||
|
|
@ -103,9 +107,16 @@ const TimeFormat = "2006-01-02T15:04"
|
|||
|
||||
// ValidateName ensures that the name is a valid format.
|
||||
func ValidateName(name string) error {
|
||||
validName := nameRegexp.Match([]byte(name))
|
||||
if validName == false {
|
||||
if nameRegexp.Match([]byte(name)) == false {
|
||||
return ErrName
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ValidateLabel ensures that the label is a valid format.
|
||||
func ValidateLabel(label string) error {
|
||||
if labelRegexp.Match([]byte(label)) == false {
|
||||
return ErrLabel
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue