| gio | e71b12b | 2026-07-29 10:02:37 +0400 | [diff] [blame^] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "unicode" |
| 6 | ) |
| 7 | |
| 8 | type ValidationError struct { |
| 9 | Field string `json:"field"` |
| 10 | Message string `json:"message"` |
| 11 | } |
| 12 | |
| 13 | func validateUsername(username string) []ValidationError { |
| 14 | var errors []ValidationError |
| 15 | if len(username) < 3 { |
| 16 | errors = append(errors, ValidationError{"username", "Username must be at least 3 characters long."}) |
| 17 | } |
| 18 | // TODO other validations |
| 19 | return errors |
| 20 | } |
| 21 | |
| 22 | func validatePassword(password string) []ValidationError { |
| 23 | var errors []ValidationError |
| 24 | if len(password) < 20 { |
| 25 | errors = append(errors, ValidationError{"password", "Password must be at least 20 characters long."}) |
| 26 | } |
| 27 | digit := false |
| 28 | lowerCase := false |
| 29 | upperCase := false |
| 30 | special := false |
| 31 | for _, c := range password { |
| 32 | if unicode.IsDigit(c) { |
| 33 | digit = true |
| 34 | } else if unicode.IsLower(c) { |
| 35 | lowerCase = true |
| 36 | } else if unicode.IsUpper(c) { |
| 37 | upperCase = true |
| 38 | } else if strings.Contains(" !\"#$%&'()*+,-./:;<=>?@[\\]^_{|}~", string(c)) { |
| 39 | special = true |
| 40 | } |
| 41 | } |
| 42 | if !digit || !lowerCase || !upperCase || !special { |
| 43 | errors = append(errors, ValidationError{"password", "Password must contain at least one digit, lower&upper case and special characters"}) |
| 44 | } |
| 45 | // TODO other validations |
| 46 | return errors |
| 47 | } |