67 lines
1.8 KiB
Go
67 lines
1.8 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"net/url"
|
|
"strings"
|
|
)
|
|
|
|
// ConfigValidator valide la configuration selon des règles strictes (T0036)
|
|
type ConfigValidator struct{}
|
|
|
|
// NewConfigValidator crée un nouveau validateur
|
|
func NewConfigValidator() *ConfigValidator {
|
|
return &ConfigValidator{}
|
|
}
|
|
|
|
// ValidatePort valide qu'un port est dans la plage valide (1-65535)
|
|
func (v *ConfigValidator) ValidatePort(port int) error {
|
|
if port < 1 || port > 65535 {
|
|
return fmt.Errorf("port must be between 1 and 65535, got %d", port)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ValidateURL valide qu'une URL a le schéma attendu
|
|
func (v *ConfigValidator) ValidateURL(urlStr, expectedScheme string) error {
|
|
if urlStr == "" {
|
|
return fmt.Errorf("URL cannot be empty")
|
|
}
|
|
|
|
parsedURL, err := url.Parse(urlStr)
|
|
if err != nil {
|
|
return fmt.Errorf("invalid URL format: %w", err)
|
|
}
|
|
|
|
if parsedURL.Scheme != expectedScheme {
|
|
return fmt.Errorf("URL must have scheme %s, got %s", expectedScheme, parsedURL.Scheme)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// ValidateEnum valide qu'une valeur fait partie des valeurs autorisées
|
|
func (v *ConfigValidator) ValidateEnum(value string, allowed []string) error {
|
|
for _, allowedValue := range allowed {
|
|
if value == allowedValue {
|
|
return nil
|
|
}
|
|
}
|
|
return fmt.Errorf("value '%s' is not allowed. Allowed values: %s", value, strings.Join(allowed, ", "))
|
|
}
|
|
|
|
// ValidateSecretLength valide qu'un secret a une longueur minimale
|
|
func (v *ConfigValidator) ValidateSecretLength(secret string, minLength int) error {
|
|
if len(secret) < minLength {
|
|
return fmt.Errorf("secret must be at least %d characters, got %d", minLength, len(secret))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ValidatePositiveInt valide qu'un entier est positif
|
|
func (v *ConfigValidator) ValidatePositiveInt(value int, fieldName string) error {
|
|
if value <= 0 {
|
|
return fmt.Errorf("%s must be positive, got %d", fieldName, value)
|
|
}
|
|
return nil
|
|
}
|