package utils import ( "crypto/rand" "encoding/base64" "fmt" "regexp" "strings" ) // GenerateRandomString generates a random string of the specified length func GenerateRandomString(length int) (string, error) { bytes := make([]byte, length) if _, err := rand.Read(bytes); err != nil { return "", err } return base64.URLEncoding.EncodeToString(bytes), nil } // ValidateEmail validates an email address using a regex func ValidateEmail(email string) bool { re := regexp.MustCompile(`^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$`) return re.MatchString(email) } // SanitizeString removes potentially dangerous characters from a string func SanitizeString(input string) string { // Remove potentially dangerous characters/sequences sanitized := strings.ReplaceAll(input, "<", "") sanitized = strings.ReplaceAll(sanitized, ">", "") sanitized = strings.ReplaceAll(sanitized, "\"", "") sanitized = strings.ReplaceAll(sanitized, "'", "") sanitized = strings.ReplaceAll(sanitized, "\\", "") sanitized = strings.ReplaceAll(sanitized, "/", "") sanitized = strings.ReplaceAll(sanitized, ";", "") sanitized = strings.ReplaceAll(sanitized, "--", "") return strings.TrimSpace(sanitized) } // IsValidRole checks if a role is valid func IsValidRole(role string) bool { switch role { case "job_seeker", "job_provider", "admin": return true default: return false } } // IsValidEmploymentType checks if an employment type is valid func IsValidEmploymentType(empType string) bool { switch empType { case "full_time", "part_time", "contract", "internship": return true default: return false } } // IsValidExperienceLevel checks if an experience level is valid func IsValidExperienceLevel(level string) bool { switch level { case "entry_level", "mid_level", "senior_level", "executive": return true default: return false } } // IsValidApplicationStatus checks if an application status is valid func IsValidApplicationStatus(status string) bool { switch status { case "pending", "reviewed", "accepted", "rejected", "open", "closed", "filled": return true default: return false } } // FormatPhoneNumber standardizes a phone number func FormatPhoneNumber(phone string) string { // Remove all non-digit characters re := regexp.MustCompile(`\D`) digits := re.ReplaceAllString(phone, "") // If it starts with country code, format accordingly if len(digits) == 11 && digits[0] == '1' { return fmt.Sprintf("(%s) %s-%s", digits[1:4], digits[4:7], digits[7:11]) } else if len(digits) == 10 { return fmt.Sprintf("(%s) %s-%s", digits[0:3], digits[3:6], digits[6:10]) } return phone // Return original if can't format }