Add comprehensive test suite
This commit is contained in:
45
internal/handlers/validation.go
Normal file
45
internal/handlers/validation.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin/binding"
|
||||
"github.com/go-playground/validator/v10"
|
||||
)
|
||||
|
||||
func init() {
|
||||
// Register custom validators
|
||||
if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
|
||||
v.RegisterValidation("alphanumdash", alphanumdashValidator)
|
||||
}
|
||||
}
|
||||
|
||||
// alphanumdashValidator validates that a string contains only alphanumeric characters and hyphens
|
||||
var alphanumdashValidator validator.Func = func(fl validator.FieldLevel) bool {
|
||||
value := fl.Field().String()
|
||||
// Allow alphanumeric and hyphens only
|
||||
match := regexp.MustCompile(`^[a-zA-Z0-9-]+$`).MatchString(value)
|
||||
return match
|
||||
}
|
||||
|
||||
// IsValidSubdomainFormat checks if a subdomain string is valid (exported for use in validation)
|
||||
func IsValidSubdomainFormat(subdomain string) bool {
|
||||
// Must be at least 3 characters
|
||||
if len(subdomain) < 3 {
|
||||
return false
|
||||
}
|
||||
|
||||
// Must not exceed 63 characters
|
||||
if len(subdomain) > 63 {
|
||||
return false
|
||||
}
|
||||
|
||||
// Must not start or end with hyphen
|
||||
if strings.HasPrefix(subdomain, "-") || strings.HasSuffix(subdomain, "-") {
|
||||
return false
|
||||
}
|
||||
|
||||
// Must contain only alphanumeric and hyphens
|
||||
return regexp.MustCompile(`^[a-zA-Z0-9-]+$`).MatchString(subdomain)
|
||||
}
|
||||
Reference in New Issue
Block a user