Add validation around Base and Scale
The Base and Scale options on axes can only be one of two parameters. We weren't validating that this was the case. This patch ensures that Base can only ever be "10" or "2", and Scale must be either "linear" or "log". Associated test coverage was also added.pull/1928/head
parent
3b51cb8339
commit
e421bf83d4
|
@ -76,14 +76,31 @@ func ValidDashboardCellRequest(c *chronograf.DashboardCell) error {
|
|||
|
||||
// HasCorrectAxes verifies that only permitted axes exist within a DashboardCell
|
||||
func HasCorrectAxes(c *chronograf.DashboardCell) error {
|
||||
for axis, _ := range c.Axes {
|
||||
switch axis {
|
||||
for label, axis := range c.Axes {
|
||||
switch label {
|
||||
case "x", "y", "y2":
|
||||
// no-op
|
||||
default:
|
||||
return chronograf.ErrInvalidAxis
|
||||
}
|
||||
|
||||
// validate axis scale
|
||||
switch axis.Scale {
|
||||
case "linear", "", "log":
|
||||
// no-op
|
||||
default:
|
||||
return chronograf.ErrInvalidAxis
|
||||
}
|
||||
|
||||
// validate axis base
|
||||
switch axis.Base {
|
||||
case "10", "2", "":
|
||||
// no-op
|
||||
default:
|
||||
return chronograf.ErrInvalidAxis
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
@ -55,6 +55,78 @@ func Test_Cells_CorrectAxis(t *testing.T) {
|
|||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"linear scale value",
|
||||
&chronograf.DashboardCell{
|
||||
Axes: map[string]chronograf.Axis{
|
||||
"x": chronograf.Axis{
|
||||
Scale: "linear",
|
||||
Bounds: []string{"0", "100"},
|
||||
},
|
||||
},
|
||||
},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"log scale value",
|
||||
&chronograf.DashboardCell{
|
||||
Axes: map[string]chronograf.Axis{
|
||||
"x": chronograf.Axis{
|
||||
Scale: "log",
|
||||
Bounds: []string{"0", "100"},
|
||||
},
|
||||
},
|
||||
},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"invalid scale value",
|
||||
&chronograf.DashboardCell{
|
||||
Axes: map[string]chronograf.Axis{
|
||||
"x": chronograf.Axis{
|
||||
Scale: "potatoes",
|
||||
Bounds: []string{"0", "100"},
|
||||
},
|
||||
},
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"base 10 axis",
|
||||
&chronograf.DashboardCell{
|
||||
Axes: map[string]chronograf.Axis{
|
||||
"x": chronograf.Axis{
|
||||
Base: "10",
|
||||
Bounds: []string{"0", "100"},
|
||||
},
|
||||
},
|
||||
},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"base 2 axis",
|
||||
&chronograf.DashboardCell{
|
||||
Axes: map[string]chronograf.Axis{
|
||||
"x": chronograf.Axis{
|
||||
Base: "2",
|
||||
Bounds: []string{"0", "100"},
|
||||
},
|
||||
},
|
||||
},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"invalid base",
|
||||
&chronograf.DashboardCell{
|
||||
Axes: map[string]chronograf.Axis{
|
||||
"x": chronograf.Axis{
|
||||
Base: "all your base are belong to us",
|
||||
Bounds: []string{"0", "100"},
|
||||
},
|
||||
},
|
||||
},
|
||||
true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range axisTests {
|
||||
|
|
Loading…
Reference in New Issue