Skip to content
Snippets Groups Projects
Commit 0e0fa5fb authored by roshan's avatar roshan
Browse files

dto validator error handling added

parent 71dc8c03
No related branches found
No related tags found
No related merge requests found
import { plainToInstance } from 'class-transformer';
import { validate } from 'class-validator';
import { validate, ValidationError } from 'class-validator';
import { RpcException } from '@nestjs/microservices'
export async function validateDto<T extends object>(
......@@ -23,3 +23,25 @@ export async function validateDto<T extends object>(
return dtoObject;
}
export function formatValidationErrors(errors: ValidationError[]): string {
const messages: string[] = [];
const extractMessages = (error: ValidationError) => {
if (error.constraints) {
// Push all constraint messages (even if from the same field)
messages.push(...Object.values(error.constraints));
}
if (error.children && error.children.length > 0) {
error.children.forEach(extractMessages);
}
};
errors.forEach(extractMessages);
return messages.join(', ');
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment