-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidation.go
More file actions
234 lines (210 loc) · 6.65 KB
/
validation.go
File metadata and controls
234 lines (210 loc) · 6.65 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package prefab
import (
"errors"
"fmt"
"net/url"
"strings"
"time"
)
// ConfigMustString returns the string value for the given key.
// It panics if the key doesn't exist or the value is empty.
//
// Example:
//
// apiKey := prefab.ConfigMustString("myapp.apiKey", "Set PF__MYAPP__API_KEY environment variable")
func ConfigMustString(key, helpMsg string) string {
if !Config.Exists(key) {
panic(fmt.Sprintf("required config '%s' not set: %s", key, helpMsg))
}
value := Config.String(key)
if value == "" {
panic(fmt.Sprintf("required config '%s' is empty: %s", key, helpMsg))
}
return value
}
// ConfigMustInt returns the int value for the given key with range validation.
// It panics if the key doesn't exist or the value is outside the given range.
//
// Example:
//
// port := prefab.ConfigMustInt("server.port", 1, 65535)
func ConfigMustInt(key string, minVal, maxVal int) int {
if !Config.Exists(key) {
panic(fmt.Sprintf("required config '%s' not set (expected %d-%d)", key, minVal, maxVal))
}
value := Config.Int(key)
if err := ValidateIntRange(value, minVal, maxVal); err != nil {
panic(fmt.Sprintf("config '%s': %v", key, err))
}
return value
}
// ConfigMustDurationRange returns the duration value for the given key with range validation.
// It panics if the key doesn't exist or the value is outside the given range.
//
// Example:
//
// timeout := prefab.ConfigMustDurationRange("myapp.timeout", time.Second, time.Minute)
func ConfigMustDurationRange(key string, minVal, maxVal time.Duration) time.Duration {
if !Config.Exists(key) {
panic(fmt.Sprintf("required config '%s' not set (expected %s-%s)", key, minVal, maxVal))
}
value := Config.Duration(key)
if err := ValidateDurationRange(value, minVal, maxVal); err != nil {
panic(fmt.Sprintf("config '%s': %v", key, err))
}
return value
}
// ValidateIntRange validates that a value is within the given range (inclusive).
func ValidateIntRange(value, minVal, maxVal int) error {
if value < minVal || value > maxVal {
return fmt.Errorf("must be between %d and %d, got: %d", minVal, maxVal, value)
}
return nil
}
// ValidateDurationRange validates that a duration is within the given range (inclusive).
func ValidateDurationRange(value, minVal, maxVal time.Duration) error {
if value < minVal || value > maxVal {
return fmt.Errorf("must be between %s and %s, got: %s", minVal, maxVal, value)
}
return nil
}
// ValidatePort validates that a port number is valid (1-65535).
func ValidatePort(port int) error {
return ValidateIntRange(port, 1, 65535)
}
// ValidatePositiveInt validates that an integer is positive (> 0).
func ValidatePositiveInt(value int) error {
if value <= 0 {
return fmt.Errorf("must be positive, got: %d", value)
}
return nil
}
// ValidatePositiveDuration validates that a duration is positive (> 0).
func ValidatePositiveDuration(value time.Duration) error {
if value <= 0 {
return fmt.Errorf("must be positive, got: %s", value)
}
return nil
}
// ValidateNonNegativeDuration validates that a duration is non-negative (>= 0).
func ValidateNonNegativeDuration(value time.Duration) error {
if value < 0 {
return fmt.Errorf("must be non-negative, got: %s", value)
}
return nil
}
// ValidateURL validates that a string is a valid URL.
func ValidateURL(urlStr string) error {
if urlStr == "" {
return errors.New("URL cannot be empty")
}
parsed, err := url.Parse(urlStr)
if err != nil {
return fmt.Errorf("invalid URL: %w", err)
}
if parsed.Scheme == "" {
return errors.New("URL must have a scheme (http:// or https://)")
}
if parsed.Host == "" {
return errors.New("URL must have a host")
}
return nil
}
// ValidateNonEmpty validates that a string is not empty.
func ValidateNonEmpty(value string) error {
if value == "" {
return errors.New("cannot be empty")
}
return nil
}
// ValidationError represents a configuration validation error.
type ValidationError struct {
Key string
Message string
}
func (e ValidationError) Error() string {
return fmt.Sprintf("%s: %s", e.Key, e.Message)
}
// ValidateConfig performs comprehensive validation of critical configuration values.
// Returns all validation errors found, or nil if configuration is valid.
//
// This should be called early in server initialization to fail fast on misconfigurations.
func ValidateConfig() []ValidationError {
var errors []ValidationError
// Validate server.port if set
if Config.Exists("server.port") {
port := Config.Int("server.port")
if err := ValidatePort(port); err != nil {
errors = append(errors, ValidationError{
Key: "server.port",
Message: err.Error(),
})
}
}
// Validate server.host is not empty if set
if Config.Exists("server.host") {
host := Config.String("server.host")
if err := ValidateNonEmpty(host); err != nil {
errors = append(errors, ValidationError{
Key: "server.host",
Message: err.Error(),
})
}
}
// Validate server.maxMsgSizeBytes if set
if Config.Exists("server.maxMsgSizeBytes") {
size := Config.Int("server.maxMsgSizeBytes")
if err := ValidatePositiveInt(size); err != nil {
errors = append(errors, ValidationError{
Key: "server.maxMsgSizeBytes",
Message: err.Error(),
})
}
}
// Validate server.security.hstsExpiration if set
if Config.Exists("server.security.hstsExpiration") {
duration := Config.Duration("server.security.hstsExpiration")
if duration > 0 { // 0 means disabled, which is valid
if err := ValidatePositiveDuration(duration); err != nil {
errors = append(errors, ValidationError{
Key: "server.security.hstsExpiration",
Message: err.Error(),
})
}
}
}
// Validate server.security.corsMaxAge if set
if Config.Exists("server.security.corsMaxAge") {
duration := Config.Duration("server.security.corsMaxAge")
if err := ValidateNonNegativeDuration(duration); err != nil {
errors = append(errors, ValidationError{
Key: "server.security.corsMaxAge",
Message: err.Error(),
})
}
}
// Validate auth.expiration if set
if Config.Exists("auth.expiration") {
duration := Config.Duration("auth.expiration")
if err := ValidatePositiveDuration(duration); err != nil {
errors = append(errors, ValidationError{
Key: "auth.expiration",
Message: err.Error(),
})
}
}
return errors
}
// FormatValidationErrors formats a slice of validation errors into a readable error message.
func FormatValidationErrors(errors []ValidationError) string {
if len(errors) == 0 {
return ""
}
var sb strings.Builder
sb.WriteString("Configuration validation failed:\n")
for _, err := range errors {
sb.WriteString(fmt.Sprintf(" - %s\n", err.Error()))
}
sb.WriteString("\nFix these errors in prefab.yaml or environment variables and try again.")
return sb.String()
}