-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidator.go
More file actions
110 lines (99 loc) · 1.95 KB
/
validator.go
File metadata and controls
110 lines (99 loc) · 1.95 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"encoding/json"
"fmt"
"strings"
"unicode"
)
func ValidateProjectName(name string) error {
if name == "" {
return fmt.Errorf("name cannot be empty")
}
if len(name) > 100 {
return fmt.Errorf("name too long")
}
if strings.ContainsAny(name, "/<>|\\") {
return fmt.Errorf("name contains invalid characters")
}
return nil
}
func ValidatePort(port int) error {
if port < 1 || port > 65535 {
return fmt.Errorf("port must be 1-65535")
}
return nil
}
func SanitizeString(s string) string {
return strings.Map(func(r rune) rune {
if unicode.IsPrint(r) {
return r
}
return -1
}, s)
}
func NormalizePath(path string) string {
path = strings.TrimSpace(path)
path = strings.ReplaceAll(path, "\\", "/")
for strings.Contains(path, "//") {
path = strings.ReplaceAll(path, "//", "/")
}
return path
}
func IsValidModel(model string) bool {
valid := []string{"haiku", "sonnet", "opus"}
for _, v := range valid {
if v == model {
return true
}
}
return false
}
func IsValidEffort(effort string) bool {
valid := []string{"low", "medium", "high", "max"}
for _, v := range valid {
if v == effort {
return true
}
}
return false
}
func ParseDuration(s string) int {
total := 0
num := 0
for _, c := range s {
if c >= '0' && c <= '9' {
num = num*10 + int(c-'0')
} else {
switch c {
case 'h':
total += num * 3600
case 'm':
total += num * 60
case 's':
total += num
}
num = 0
}
}
return total + num
}
func ValidateTimeout(seconds int) error {
if seconds < 0 {
return fmt.Errorf("timeout cannot be negative")
}
if seconds > 3600 {
return fmt.Errorf("timeout too large (max 1h)")
}
return nil
}
func SanitizeURL(url string) string {
url = strings.TrimSpace(url)
if !strings.HasPrefix(url, "http://") && !strings.HasPrefix(url, "https://") {
url = "https://" + url
}
return url
}
func ValidateJSON(data []byte) bool {
var v interface{}
return json.Unmarshal(data, &v) == nil
}