Enhance Customer Module with Validation and Username Handling

- 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.
This commit is contained in:
n.nakhostin
2025-09-24 12:57:23 +03:30
parent 080c1d18e5
commit 939f940499
6 changed files with 65 additions and 9 deletions
+41
View File
@@ -0,0 +1,41 @@
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)
}