admin管理员组文章数量:1023064
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
1 Answer
Reset to default 2If 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;
}
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
1 Answer
Reset to default 2If 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;
}
本文标签: javascriptNestjs Class Validator for Confirm PasswordStack Overflow
版权声明:本文标题:javascript - Nestjs Class Validator for Confirm Password - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745574052a2156909.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论