-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvalidator.go
More file actions
48 lines (40 loc) · 1.5 KB
/
validator.go
File metadata and controls
48 lines (40 loc) · 1.5 KB
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
package bulwark
import (
"fmt"
"log/slog"
"github.com/deixis/faults"
)
var (
// AssertValidPriority panics when a priority is out of range.
// A priority is out of range when it is less than 0 or greater than or equal
// to priorities.
AssertValidPriority = func(p Priority, priorities int) (Priority, error) {
if p < 0 || int(p) >= priorities {
panic(fmt.Sprintf("bulwark: priority must be in the range [0, %d), but got %d", priorities, p))
}
return p, nil
}
// ClampInvalidPriority clamps any out-of-range priority to the lowest valid
// priority (priorities-1). This applies to both negative values and values
// that exceed the configured number of priorities, preventing invalid or
// malicious input from being promoted to a higher-importance tier.
ClampInvalidPriority = func(p Priority, priorities int) (Priority, error) {
if p >= 0 && int(p) < priorities {
return p, nil
}
slog.Warn("bulwark: priority is out of range", "max", priorities-1, "priority", p)
return Priority(priorities - 1), nil
}
// RejectInvalidPriority returns an error when a priority is out of range.
// A priority is out of range when it is less than 0 or greater than or equal
// to priorities.
RejectInvalidPriority = func(p Priority, priorities int) (Priority, error) {
if p < 0 || int(p) >= priorities {
return p, faults.Bad(&faults.FieldViolation{
Field: "priority",
Description: fmt.Sprintf("priority must be in the range [0, %d), but got %d", priorities, p),
})
}
return p, nil
}
)