Files
tm_back/internal/kanban/validation.go
T
2026-01-05 15:36:20 +03:30

42 lines
1.1 KiB
Go

package kanban
import (
"regexp"
"github.com/asaskevich/govalidator"
)
// ValidationService defines validation methods for kanban operations
type ValidationService interface {
IsValidHexColor(hexColor string) bool
}
// validationService implements the ValidationService interface
type validationService struct {
hexColorRegex *regexp.Regexp
}
// NewValidationService creates a new validation service
func NewValidationService() ValidationService {
// Compile the hex color regex pattern (supports #RGB, #RRGGBB, #RRGGBBAA)
hexColorRegex := regexp.MustCompile(`^#([A-Fa-f0-9]{3}|[A-Fa-f0-9]{6}|[A-Fa-f0-9]{8})$`)
// Register custom validator with govalidator
govalidator.CustomTypeTagMap.Set("hexcolor", govalidator.CustomTypeValidator(func(i interface{}, o interface{}) bool {
hexColor, ok := i.(string)
if !ok {
return false
}
return hexColorRegex.MatchString(hexColor)
}))
return &validationService{
hexColorRegex: hexColorRegex,
}
}
// IsValidHexColor checks if a string is a valid hex color code
func (v *validationService) IsValidHexColor(hexColor string) bool {
return v.hexColorRegex.MatchString(hexColor)
}