This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
/
validator.go
61 lines (51 loc) · 1.43 KB
/
validator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package expr
import (
"github.com/grafana/metrictank/consolidation"
"github.com/grafana/metrictank/errors"
"github.com/raintank/dur"
)
var ErrIntPositive = errors.NewBadRequest("integer must be positive")
var ErrInvalidAggFunc = errors.NewBadRequest("Invalid aggregation func")
var ErrNonNegativePercent = errors.NewBadRequest("The requested percent is required to be greater than 0")
var ErrWithinZeroOneInclusiveInterval = errors.NewBadRequest("value must lie within interval [0,1]")
// Validator is a function to validate an input
type Validator func(e *expr) error
// IntPositive validates whether an int is positive (greater than zero)
func IntPositive(e *expr) error {
if e.int < 1 {
return ErrIntPositive
}
return nil
}
func IsAggFunc(e *expr) error {
if getCrossSeriesAggFunc(e.str) == nil {
return ErrInvalidAggFunc
}
return nil
}
func IsConsolFunc(e *expr) error {
return consolidation.Validate(e.str)
}
func IsIntervalString(e *expr) error {
_, err := dur.ParseDuration(e.str)
return err
}
func IsOperator(e *expr) error {
switch e.str {
case "=", "!=", ">", ">=", "<", "<=":
return nil
}
return errors.NewBadRequest("Unsupported operator: " + e.str)
}
func NonNegativePercent(e *expr) error {
if e.float < 0 || e.int < 0 {
return ErrNonNegativePercent
}
return nil
}
func WithinZeroOneInclusiveInterval(e *expr) error {
if e.float < 0 || e.float > 1 {
return ErrWithinZeroOneInclusiveInterval
}
return nil
}