Skip to content

Commit

Permalink
fix: Target validation
Browse files Browse the repository at this point in the history
There was a bug: user could create trigger with target with space in metric name like `a b`. Now validation doesn't allow it.
  • Loading branch information
Dimedrolity committed Dec 5, 2022
1 parent 718f1b6 commit 2ec1ec9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
8 changes: 7 additions & 1 deletion api/dto/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,18 @@ func TargetVerification(targets []string, ttl time.Duration, isRemote bool) []Tr
for _, target := range targets {
functionsOfTarget := TreeOfProblems{SyntaxOk: true}

expr, _, err := parser.ParseExpr(target)
expr, nestedExpr, err := parser.ParseExpr(target)
if err != nil {
functionsOfTarget.SyntaxOk = false
functionsOfTargets = append(functionsOfTargets, functionsOfTarget)
continue
}
isSpaceInMetricName := nestedExpr != ""
if isSpaceInMetricName {
functionsOfTarget.SyntaxOk = false
functionsOfTargets = append(functionsOfTargets, functionsOfTarget)
continue
}

functionsOfTarget.TreeOfProblems = checkExpression(expr, ttl, isRemote)
functionsOfTargets = append(functionsOfTargets, functionsOfTarget)
Expand Down
7 changes: 7 additions & 0 deletions api/dto/target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ func TestTargetVerification(t *testing.T) {
So(problems[0].SyntaxOk, ShouldBeTrue)
So(problems[0].TreeOfProblems, ShouldBeNil)
})

Convey("Check target with space symbol in metric name", func() {
targets := []string{"a b"}
problems := TargetVerification(targets, 0, false)
So(problems[0].SyntaxOk, ShouldBeFalse)
So(problems[0].TreeOfProblems, ShouldBeNil)
})
})
}

Expand Down

0 comments on commit 2ec1ec9

Please sign in to comment.