Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicate validation messages #1

Open
frankly034 opened this issue Jan 24, 2023 · 0 comments
Open

Duplicate validation messages #1

frankly034 opened this issue Jan 24, 2023 · 0 comments

Comments

@frankly034
Copy link

frankly034 commented Jan 24, 2023

Hello @Mohammad-Faisal, nice simple to follow tutorial.

I noticed an issue with the RequestValidator; it duplicates the validation error messages. See screenshot below.

Screenshot 2023-01-24 at 09 33 18

The expected message should look like this:

Screenshot 2023-01-24 at 09 33 18

And was fixed with this change:

export default class RequestValidator {
  static validate = <T extends object>(classInstance: ClassConstructor<T>) => {
    return async (req: Request, res: Response, next: NextFunction) => {
      const convertedObject = plainToInstance(classInstance, req.body);
      const errors = await validate(convertedObject);
      if (errors.length > 0) {
        let rawErrors: string[] = [];
        for (const errorItem of errors) {
          rawErrors = rawErrors.concat(...Object.values(errorItem.constraints ?? []));
        }
        const validationErrorText = 'Request validation failed!';
        console.log('error found!', rawErrors);
        next(new BadRequestError(validationErrorText, rawErrors));
      }
      next();
    };
  };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant