Files
web/packages/common/utils/zod/passwordValidator.ts
Tobias Johansson f0701d6e20 Merged in fix/SW-3111-mismatch-password-rules (pull request #2482)
fix(SW-3111): Added test for passwordValidator and updated pw validation

* fix(SW-3111): Added test for passwordValidator and updated pw validation

* fix: make sure regex is matching Curity

* Added error message to input field and changed message

* Fixed text


Approved-by: Christian Andolf
2025-07-01 05:51:31 +00:00

54 lines
1.7 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,
})