Skip to content

Commit

Permalink
Added new type for FeatureValueLabels (#62)
Browse files Browse the repository at this point in the history
This will be used in Marketplace and across clients, including Grafton

Relates manifoldco/engineering#4858
  • Loading branch information
tim-speed committed Jul 9, 2018
1 parent 4409c63 commit e262a63
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
21 changes: 18 additions & 3 deletions formats.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import (
)

var (
labelRegex = regexp.MustCompile("^[a-z0-9][a-z0-9-_]{1,128}$")
nameRegex = regexp.MustCompile(`^[a-zA-Z0-9][a-z0-9A-Z\. \-_]{2,128}$`)
codeRegex = regexp.MustCompile("^([0-9abcdefghjkmnpqrtuvwxyz]{16}|[0-9]{6})$")
labelRegex = regexp.MustCompile("^[a-z0-9][a-z0-9-_]{1,128}$")
nameRegex = regexp.MustCompile(`^[a-zA-Z0-9][a-z0-9A-Z\. \-_]{2,128}$`)
codeRegex = regexp.MustCompile("^([0-9abcdefghjkmnpqrtuvwxyz]{16}|[0-9]{6})$")
featureValueLabelRegex = regexp.MustCompile(`^[a-z0-9][a-z0-9-_\.]{1,128}$`)
)

var (
Expand All @@ -20,6 +21,8 @@ var (
errInvalidEmail = NewError(errors.BadRequestError, "Invalid Email Provided")
errInvalidCode = NewError(errors.BadRequestError,
"Invalid email verification code provided")
errInvalidFeatureValueLabel = NewError(errors.BadRequestError,
"Invalid feature value label provided")
)

// Label represents any object's label field
Expand Down Expand Up @@ -69,3 +72,15 @@ func (c Code) Validate(_ interface{}) error {

return errInvalidCode
}

// FeatureValueLabel represents any object's label field
type FeatureValueLabel string

// Validate ensures the label value is valid
func (lbl FeatureValueLabel) Validate(_ interface{}) error {
if featureValueLabelRegex.Match([]byte(lbl)) {
return nil
}

return errInvalidFeatureValueLabel
}
43 changes: 43 additions & 0 deletions formats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,46 @@ func TestCode(t *testing.T) {
}
})
}

func TestFeatureValueLabel(t *testing.T) {
t.Run("errors on invalid feature value label", func(t *testing.T) {
l := FeatureValueLabel("BluesClues")
err := l.Validate(nil)
if err == nil {
t.Error("Expected an error")
}
l = FeatureValueLabel("blues clues")
err = l.Validate(nil)
if err == nil {
t.Error("Expected an error")
}
l = FeatureValueLabel("moosejuice!")
err = l.Validate(nil)
if err == nil {
t.Error("Expected an error")
}

_, ok := err.(*Error)
if !ok {
t.Error("Expected a manifold Error")
}
})

t.Run("does not error on valid feature value label", func(t *testing.T) {
l := FeatureValueLabel("t2.micro")
err := l.Validate(nil)
if err != nil {
t.Errorf("Unexpected Error: %s", err)
}
l = FeatureValueLabel("fat-cats")
err = l.Validate(nil)
if err != nil {
t.Errorf("Unexpected Error: %s", err)
}
l = FeatureValueLabel("orange_soda")
err = l.Validate(nil)
if err != nil {
t.Errorf("Unexpected Error: %s", err)
}
})
}

0 comments on commit e262a63

Please sign in to comment.