-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconstraint_values.go
More file actions
59 lines (49 loc) · 1.31 KB
/
constraint_values.go
File metadata and controls
59 lines (49 loc) · 1.31 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
49
50
51
52
53
54
55
56
57
58
59
package CADDY_FILE_SERVER
import (
"errors"
"fmt"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"slices"
"strconv"
)
func init() {
RegisterConstraintType(func() Constraint {
return new(ValuesConstraint)
})
}
type ValuesConstraint struct {
Values []int `json:"values"`
}
func (r *ValuesConstraint) ID() string {
return "values"
}
func (r *ValuesConstraint) Validate(param string) error {
if !slices.Contains([]string{"w", "h", "q", "ah", "aw", "t", "l", "r", "b"}, param) {
return fmt.Errorf("values constraint cannot be applied on param: '%s'", param)
}
if len(r.Values) == 0 {
return errors.New("you need to provide at least one value for values constraint")
}
return nil
}
func (r *ValuesConstraint) ValidateParam(param string, value string) error {
intValue, err := strconv.Atoi(value)
if err != nil {
return fmt.Errorf("invalid integer value for %s: %s", param, value)
}
if !slices.Contains(r.Values, intValue) {
return fmt.Errorf("parameter %s has an invalid value: %d", param, intValue)
}
return nil
}
func (r *ValuesConstraint) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
values := d.RemainingArgs()
r.Values = make([]int, len(values))
for idx, v := range values {
var err error
if r.Values[idx], err = strconv.Atoi(v); err != nil {
return err
}
}
return nil
}