mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-15 18:27:05 +00:00
add password validator
This commit is contained in:
33
src/validators/password.validator.ts
Normal file
33
src/validators/password.validator.ts
Normal file
@ -0,0 +1,33 @@
|
||||
import {
|
||||
registerDecorator,
|
||||
ValidationOptions,
|
||||
ValidatorConstraint,
|
||||
ValidatorConstraintInterface,
|
||||
} from 'class-validator';
|
||||
|
||||
@ValidatorConstraint({ async: false })
|
||||
export class IsPasswordStrongConstraint
|
||||
implements ValidatorConstraintInterface
|
||||
{
|
||||
validate(password: string) {
|
||||
const regex =
|
||||
/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/;
|
||||
return regex.test(password);
|
||||
}
|
||||
|
||||
defaultMessage() {
|
||||
return 'password must be at least 8 characters long and include at least one uppercase letter, one lowercase letter, one numeric digit, and one special character.';
|
||||
}
|
||||
}
|
||||
|
||||
export function IsPasswordStrong(validationOptions?: ValidationOptions) {
|
||||
return function (object: Record<string, any>, propertyName: string) {
|
||||
registerDecorator({
|
||||
target: object.constructor,
|
||||
propertyName: propertyName,
|
||||
options: validationOptions,
|
||||
constraints: [],
|
||||
validator: IsPasswordStrongConstraint,
|
||||
});
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user