Created
July 16, 2022 16:32
-
-
Save Klerith/c77edd9d86174aa68efbfddd21f053a2 to your computer and use it in GitHub Desktop.
Password validation - DTO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@IsString() | |
@MinLength(6) | |
@MaxLength(50) | |
@Matches( | |
/(?:(?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$/, { | |
message: 'The password must have a Uppercase, lowercase letter and a number' | |
}) | |
password: string; |
:v
Buenas profe, una consulta.
¿En este caso se podria agregar a la expresion regular la limitacion de longitud verdad?
Pero entiendo que se elige usar @minlength y @maxlength por el error especifico que devuelve nest en caso de que esa restriccion se rompa, verdad?
Muchas gracias
Genial, muchas gracias (=
Un éxito, gracias Ferrrrrrr
{ registerDecorator,
ValidationOptions,
ValidationArguments,
} from 'class-validator';
export function IsEqualTo(
property: string,
validationOptions?: ValidationOptions,
) {
return function (object: any, propertyName: string) {
registerDecorator({
name: 'isEqualTo',
target: object.constructor,
propertyName: propertyName,
constraints: [property],
options: validationOptions,
validator: {
validate(value: any, args: ValidationArguments) {
const relatedValue = (args.object as any)[args.constraints[0]];
return value === relatedValue;
},
},
});
};
}
//------------------------------------------
@IsString()
@MinLength(8)
@MaxLength(50)
@Matches(
/(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*\W)/,
{ message: 'The password must include uppercase, lowercase letters, a number, and a special character.' }
)
password: string;
@IsString()
@IsEqualTo('password', {
message: 'Password confirmation does not match with password',
})
passwordConfirm: string;
Bendiciones
Muchas gracias! 🤝
import {
IsStrongPassword,
} from 'class-validator';
Recomiendo usar este decorador para controlar la fortaleza de la password. Ajustar los parámetros.
@IsStrongPassword({
minLength: 6,
minLowercase: 1,
minUppercase: 1,
minNumbers: 1,
minSymbols: 0,
})
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Excelente pattern profe, ¡gracias!