Files
web/packages/common/utils/zod/passwordValidator.ts
Linus Flood 1b95acad29 Merged in fix/password-remove-hash (pull request #2487)
Remove # from allowed characters in password field

* Remove # from allowed characters in password field

* Fixed tests

* Fixed tests
2025-07-01 07:19:50 +00:00

54 lines
1.6 KiB
TypeScript

import { z } from "zod"
export const passwordValidators = {
length: {
matcher: (password: string) =>
password.length >= 10 && password.length <= 40,
message: "10 to 40 characters",
},
hasUppercase: {
matcher: (password: string) => /[A-ZÅÄÖÆØÜ]/.test(password),
message: "1 uppercase letter",
},
hasLowercase: {
matcher: (password: string) => /[a-zåäöæøüß]/.test(password),
message: "1 lowercase letter",
},
hasNumber: {
matcher: (password: string) => /[0-9]/.test(password),
message: "1 number",
},
hasSpecialChar: {
matcher: (password: string) => /[&!?()@#$%^+=_\*\-]+/.test(password),
message: "1 special character",
},
allowedCharacters: {
matcher: (password: string) =>
/^[A-Za-zåäöæøüßÅÄÖÆØÜ0-9&!?()@$%^+=_\*\-]+$/.test(password),
message: "Only allowed characters",
},
}
export const passwordValidator = (msg = "Required field") =>
z
.string()
.min(1, msg)
.refine(passwordValidators.allowedCharacters.matcher, {
message: passwordValidators.allowedCharacters.message,
})
.refine(passwordValidators.length.matcher, {
message: passwordValidators.length.message,
})
.refine(passwordValidators.hasUppercase.matcher, {
message: passwordValidators.hasUppercase.message,
})
.refine(passwordValidators.hasLowercase.matcher, {
message: passwordValidators.hasLowercase.message,
})
.refine(passwordValidators.hasNumber.matcher, {
message: passwordValidators.hasNumber.message,
})
.refine(passwordValidators.hasSpecialChar.matcher, {
message: passwordValidators.hasSpecialChar.message,
})