939f940499
- Introduced a new ValidationService for customer operations to ensure valid username formats. - Updated customer service and handler to utilize the new validation service, enhancing input validation during registration and updates. - Modified CreateCustomerForm and UpdateCustomerForm to enforce username validation rules. - Adjusted repository indexing to improve search efficiency by refining the text index for customer data. - Ensured consistent handling of usernames across the customer module, improving overall data integrity and user experience.
42 lines
1.1 KiB
Go
42 lines
1.1 KiB
Go
package customer
|
|
|
|
import (
|
|
"regexp"
|
|
|
|
"github.com/asaskevich/govalidator"
|
|
)
|
|
|
|
// ValidationService defines validation methods for customer operations
|
|
type ValidationService interface {
|
|
IsValidUsername(username string) bool
|
|
}
|
|
|
|
// validationService implements the ValidationService interface
|
|
type validationService struct {
|
|
usernameRegex *regexp.Regexp
|
|
}
|
|
|
|
// NewValidationService creates a new validation service
|
|
func NewValidationService() ValidationService {
|
|
// Compile the username regex pattern
|
|
usernameRegex := regexp.MustCompile(`^[a-zA-Z0-9](?:[a-zA-Z0-9._]*[a-zA-Z0-9])?$`)
|
|
|
|
// Register custom validator with govalidator
|
|
govalidator.CustomTypeTagMap.Set("username", govalidator.CustomTypeValidator(func(i interface{}, o interface{}) bool {
|
|
username, ok := i.(string)
|
|
if !ok {
|
|
return false
|
|
}
|
|
return usernameRegex.MatchString(username)
|
|
}))
|
|
|
|
return &validationService{
|
|
usernameRegex: usernameRegex,
|
|
}
|
|
}
|
|
|
|
// IsValidUsername checks if a username matches the required pattern
|
|
func (v *validationService) IsValidUsername(username string) bool {
|
|
return v.usernameRegex.MatchString(username)
|
|
}
|