refactor: password requirements variables;

refactor: password validation function moved to custom validators;
refactor: adjusted model's validation fields
This commit is contained in:
2025-07-05 17:50:01 +03:00
parent 8319afc7ea
commit 5e32c3cbd3
4 changed files with 62 additions and 68 deletions

View File

@@ -18,6 +18,7 @@
package validation
import (
"easywish/config"
"regexp"
"github.com/go-playground/validator/v10"
@@ -46,6 +47,36 @@ func GetCustomHandlers() []CustomValidatorHandler {
return regexp.MustCompile(`^[.]{1,75}$`).MatchString(username)
}},
{
FieldName: "password",
Function: func(fl validator.FieldLevel) bool {
password := fl.Field().String()
cfg := config.GetConfig()
if cfg.PasswordMaxLength < len(password) || len(password) < cfg.PasswordMinLength {
return false
}
if cfg.PasswordCheckNumbers && !regexp.MustCompile(`\d+`).MatchString(password) {
return false
}
if cfg.PasswordCheckCases && !regexp.MustCompile(`^(?=.*[a-z])(?=.*[A-Z]).*$`).MatchString(password) ||
cfg.PasswordCheckCharacters && !regexp.MustCompile(`[a-zA-Z]`).MatchString(password) {
return false
}
if cfg.PasswordCheckSymbols && !regexp.MustCompile(`[.,/;'[\]\-=_+{}:"<>?\/|!@#$%^&*()~]`).MatchString(password) {
return false
}
if cfg.PasswordCheckLeaked {
// TODO: implement rockme check
}
return true
}},
}
return handlers