-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
49 lines (41 loc) · 1.52 KB
/
errors.go
File metadata and controls
49 lines (41 loc) · 1.52 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
package rigging
import (
"fmt"
"strings"
)
// Error codes for validation failures.
const (
ErrCodeRequired = "required" // Field is required but not provided
ErrCodeMin = "min" // Value is below minimum constraint
ErrCodeMax = "max" // Value exceeds maximum constraint
ErrCodeOneOf = "oneof" // Value is not in the allowed set
ErrCodeInvalidType = "invalid_type" // Type conversion failed
ErrCodeUnknownKey = "unknown_key" // Configuration key doesn't map to any field (strict mode)
ErrCodeInvalidTag = "invalid_tag" // Struct tag directive is malformed or unrecognized
)
// ValidationError aggregates field-level validation failures.
type ValidationError struct {
FieldErrors []FieldError
}
// Error formats validation errors as a multi-line message.
func (e *ValidationError) Error() string {
if len(e.FieldErrors) == 0 {
return "config validation failed: no errors"
}
var b strings.Builder
if len(e.FieldErrors) == 1 {
b.WriteString("config validation failed: 1 error\n")
} else {
fmt.Fprintf(&b, "config validation failed: %d errors\n", len(e.FieldErrors))
}
for _, fe := range e.FieldErrors {
fmt.Fprintf(&b, " - %s: %s (%s)\n", fe.FieldPath, fe.Code, fe.Message)
}
return strings.TrimRight(b.String(), "\n")
}
// FieldError represents a single field validation failure.
type FieldError struct {
FieldPath string // Dot notation (e.g., "Database.Host")
Code string // Error code (e.g., "required", "min")
Message string // Human-readable description
}