Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

Added decorator for password validation

Yevhenii Kulisidi127b24f8ab5a
diff --git a/src/auth/decorators/index.ts b/src/auth/decorators/index.ts
new file mode 100644
index 000000000..2ec2291fc
--- /dev/null
+++ b/src/auth/decorators/index.ts
@@ -0,0 +1 @@
+export * from './validators'
diff --git a/src/auth/decorators/validators/index.ts b/src/auth/decorators/validators/index.ts
new file mode 100644
index 000000000..de4a708f5
--- /dev/null
+++ b/src/auth/decorators/validators/index.ts
@@ -0,0 +1 @@
+export * from './is-password.validator'
diff --git a/src/auth/decorators/validators/is-password.validator.ts b/src/auth/decorators/validators/is-password.validator.ts
new file mode 100644
index 000000000..ee4ceb3d1
--- /dev/null
+++ b/src/auth/decorators/validators/is-password.validator.ts
@@ -0,0 +1,30 @@
+import {
+ registerDecorator,
+ ValidationOptions,
+ ValidatorConstraint,
+ ValidatorConstraintInterface,
+} from 'class-validator'
+
+@ValidatorConstraint()
+class IsPasswordConstraint implements ValidatorConstraintInterface {
+ private readonly passwordRegex =
+ /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*])[a-zA-Z\d!@#$%^&*]{8,72}$/
+
+ public validate(password: string): boolean {
+ return this.passwordRegex.test(password)
+ }
+}
+
+export function IsPassword(
+ validationOptions?: ValidationOptions,
+): PropertyDecorator {
+ return function (object: object, propertyName: string) {
+ registerDecorator({
+ propertyName,
+ target: object.constructor,
+ validator: IsPasswordConstraint,
+ constraints: [],
+ options: validationOptions,
+ })
+ }
+}
diff --git a/src/auth/dtos/register.dto.ts b/src/auth/dtos/register.dto.ts
index 1bb386c7a..b5eebcbd0 100644
--- a/src/auth/dtos/register.dto.ts
+++ b/src/auth/dtos/register.dto.ts
@@ -1,13 +1,12 @@
-import { IsBoolean, IsEmail, IsNotEmpty, Matches } from 'class-validator'
+import { IsBoolean, IsEmail, IsNotEmpty } from 'class-validator'
+import { IsPassword } from '../decorators'
export namespace RegisterDto {
export class Request {
@IsEmail()
public readonly email: string
- @Matches(
- /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*])[a-zA-Z\d!@#$%^&*]{8,72}$/,
- )
+ @IsPassword()
public readonly password: string
@IsNotEmpty()