Refactor set membership validation
The pattern of using a select with a list of options and a default that returns an error isn't bad for a one-off validation: select myProp { case "validOption1", "validOption2": // no-op default: panic("invalid!") } However, we're doing this multiple times in this method, so it makes sense to pull this out into a new method to make it clearer what's happening. This adds a `oneOf` function that takes some property and a variadic list of valid options and reports whether or not that property is among that list.pull/1928/head
parent
e421bf83d4
commit
d559acc0e0
|
@ -77,26 +77,15 @@ func ValidDashboardCellRequest(c *chronograf.DashboardCell) error {
|
|||
// HasCorrectAxes verifies that only permitted axes exist within a DashboardCell
|
||||
func HasCorrectAxes(c *chronograf.DashboardCell) error {
|
||||
for label, axis := range c.Axes {
|
||||
switch label {
|
||||
case "x", "y", "y2":
|
||||
// no-op
|
||||
default:
|
||||
if !oneOf(label, "x", "y", "y2") {
|
||||
return chronograf.ErrInvalidAxis
|
||||
}
|
||||
|
||||
// validate axis scale
|
||||
switch axis.Scale {
|
||||
case "linear", "", "log":
|
||||
// no-op
|
||||
default:
|
||||
if !oneOf(axis.Scale, "linear", "log", "") {
|
||||
return chronograf.ErrInvalidAxis
|
||||
}
|
||||
|
||||
// validate axis base
|
||||
switch axis.Base {
|
||||
case "10", "2", "":
|
||||
// no-op
|
||||
default:
|
||||
if !oneOf(axis.Base, "10", "2", "") {
|
||||
return chronograf.ErrInvalidAxis
|
||||
}
|
||||
}
|
||||
|
@ -104,6 +93,17 @@ func HasCorrectAxes(c *chronograf.DashboardCell) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// oneOf reports whether a provided string is a member of a variadic list of
|
||||
// valid options
|
||||
func oneOf(prop string, validOpts ...string) bool {
|
||||
for _, valid := range validOpts {
|
||||
if prop == valid {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// CorrectWidthHeight changes the cell to have at least the
|
||||
// minimum width and height
|
||||
func CorrectWidthHeight(c *chronograf.DashboardCell) {
|
||||
|
|
Loading…
Reference in New Issue