最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Nestjs Class Validator for Confirm Password - Stack Overflow

programmeradmin2浏览0评论

I am working on a change password endpoint using nestjs , I want to be able to validate if the two password match

The class-validator library doesn't seem to have that on how to validate two properties within the DTO

How do I achieve this

This is my current code

export class CreatePasswordDTO {
  @MaxLength(6)
  @IsString()
  @Matches(/^\d{6}$/, { message: "Password must be a 6-digit number" })
  password: string;

  @MaxLength(6)
  @IsString()
  @Matches(/^\d{6}$/, { message: "Confirm Password must be a 6-digit number" })
  // I check that the password and confirmPassword are the same
  confirmPassword: string;
}

I am working on a change password endpoint using nestjs , I want to be able to validate if the two password match

The class-validator library doesn't seem to have that on how to validate two properties within the DTO

How do I achieve this

This is my current code

export class CreatePasswordDTO {
  @MaxLength(6)
  @IsString()
  @Matches(/^\d{6}$/, { message: "Password must be a 6-digit number" })
  password: string;

  @MaxLength(6)
  @IsString()
  @Matches(/^\d{6}$/, { message: "Confirm Password must be a 6-digit number" })
  // I check that the password and confirmPassword are the same
  confirmPassword: string;
}
Share Improve this question asked Nov 19, 2024 at 8:35 Ugwu SomtoUgwu Somto 4402 silver badges11 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

If there is no default solution, what you usually do is come up with a custom implementation. I would create custom validator to check related fields at the same time:

import { registerDecorator, ValidationArguments, ValidationOptions } from 'class-validator';

export function Match(property: string, validationOptions?: ValidationOptions) {
  return (object: any, propertyName: string) => {
    registerDecorator({
      name: 'Match',
      target: object.constructor,
      propertyName: propertyName,
      options: validationOptions,
      constraints: [property],
      validator: {
        validate(value: any, args: ValidationArguments) {
          const [relatedPropertyName] = args.constraints;
          const relatedValue = (args.object as any)[relatedPropertyName];
          return value === relatedValue;
        },
        defaultMessage(args: ValidationArguments) {
          const [relatedPropertyName] = args.constraints;
          return `${args.property} must match ${relatedPropertyName}`;
        },
      },
    });
  };
}

And use your custom validator on confirmPassword field:

import { IsString, MaxLength, Matches } from 'class-validator';
import { Match } from './match.decorator'; // Adjust the import path as needed

export class CreatePasswordDTO {
  @MaxLength(6)
  @IsString()
  @Matches(/^\d{6}$/, { message: 'Password must be a 6-digit number' })
  password: string;

  @MaxLength(6)
  @IsString()
  @Matches(/^\d{6}$/, { message: 'Confirm Password must be a 6-digit number' })
  @Match('password', { message: 'Passwords do not match' })
  confirmPassword: string;
}
发布评论

评论列表(0)

  1. 暂无评论