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

Feature Request : Support for validationMiddleware on query params #40

Closed
malavancs opened this issue Nov 9, 2020 · 3 comments · Fixed by #42
Closed

Feature Request : Support for validationMiddleware on query params #40

malavancs opened this issue Nov 9, 2020 · 3 comments · Fixed by #42
Labels
⚡ Feature Suggest an idea for this project

Comments

@malavancs
Copy link
Contributor

Currently validation middleware only works on req.body. It will be good if have an option to specify what needs to validated.

@ljlm0402
Copy link
Owner

@malavancs

If it is an option you specify, do you mean req.query, req.params?

@ljlm0402 ljlm0402 added the ⚡ Feature Suggest an idea for this project label Nov 10, 2020
@malavancs
Copy link
Contributor Author

Currently in plainToClass(type,req.body) is default. It should allow us to specify whether it is a body or query or params in function parameters.

Its a nice have option I guess.

function validationMiddleware(type: any, skipMissingProperties = false): RequestHandler {
  return (req, res, next) => {
    validate(plainToClass(type, req.body), { skipMissingProperties })
    .then((errors: ValidationError[]) => {
      if (errors.length > 0) {
        const message = errors.map((error: ValidationError) => Object.values(error.constraints)).join(', ');
        next(new HttpException(400, message));
      } else {
        next();
      }snippets
    });
  };
}

PS: I will make a pull request for this soon .

@ljlm0402
Copy link
Owner

@malavancs

Thank you Bro.

@ljlm0402 ljlm0402 linked a pull request Nov 18, 2020 that will close this issue
4 tasks
ljlm0402 added a commit that referenced this issue Nov 18, 2020
#40 Added validationMiddleware support for query and params
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
⚡ Feature Suggest an idea for this project
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants