| package main |
| |
| import ( |
| "strings" |
| "unicode" |
| ) |
| |
| type ValidationError struct { |
| Field string `json:"field"` |
| Message string `json:"message"` |
| } |
| |
| func validateUsername(username string) []ValidationError { |
| var errors []ValidationError |
| if len(username) < 3 { |
| errors = append(errors, ValidationError{"username", "Username must be at least 3 characters long."}) |
| } |
| // TODO other validations |
| return errors |
| } |
| |
| func validatePassword(password string) []ValidationError { |
| var errors []ValidationError |
| if len(password) < 20 { |
| errors = append(errors, ValidationError{"password", "Password must be at least 20 characters long."}) |
| } |
| digit := false |
| lowerCase := false |
| upperCase := false |
| special := false |
| for _, c := range password { |
| if unicode.IsDigit(c) { |
| digit = true |
| } else if unicode.IsLower(c) { |
| lowerCase = true |
| } else if unicode.IsUpper(c) { |
| upperCase = true |
| } else if strings.Contains(" !\"#$%&'()*+,-./:;<=>?@[\\]^_{|}~", string(c)) { |
| special = true |
| } |
| } |
| if !digit || !lowerCase || !upperCase || !special { |
| errors = append(errors, ValidationError{"password", "Password must contain at least one digit, lower&upper case and special characters"}) |
| } |
| // TODO other validations |
| return errors |
| } |