Skn0tt / nextjs-nestjs-integration-example

https://nextjs-nestjs-integration-example.now.sh
156 stars 36 forks source link

Enable NestJS' ValidationPipe feature #22

Closed szabolcs-szilagyi closed 3 years ago

szabolcs-szilagyi commented 3 years ago

Apart of adding the babel-plugin-transform-typescript-metadata plugin it was also needed to upgrade typescript in the dependencies as well from v3 to v4. With v3 the validation was throwing error even when all the parameters were provided correctly.


Original issue description:

Setting up the validation as described in NestJS documentation would expect to get BadRequestException, but it doesn't take it into consideration.

Way I tried can be found here: https://github.com/Skn0tt/nextjs-nestjs-integration-example/compare/master...szabolcs-szilagyi:validation-pipe?expand=1

$ curl --no-progress-meter 'http://localhost:3000/api/randomNumber/pipe-test?randomNumber=33' | jq .
{
  "numberNameDto": {
    "randomNumber": "33",
    "catchAll": [
      "randomNumber",
      "pipe-test"
    ]
  }
}

Tried the same setup with a new nestjs app there it worked. Also oddly it looks like the dto is completely ignored in the function signature: we also get the catchAll parameter.

Think this is some babel magic again, but not sure.

szabolcs-szilagyi commented 3 years ago

interestingly if I add this to the babelrc, then I do get the error - even if I provide the query params: https://github.com/leonardfactory/babel-plugin-transform-typescript-metadata

$ curl --no-progress-meter 'http://localhost:3000/api/randomNumber/pipe-test?randomNumber=33&name=asdf' | jq .
{
  "statusCode": 400,
  "message": [
    "randomNumber must be a string",
    "name must be a string"
  ],
  "error": "Bad Request"
}
Skn0tt commented 3 years ago

Okay, so you propose adding babel-plugin-transform-typescript-metadata? Feel free to open a PR :)

vercel[bot] commented 3 years ago

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/skn0tt/nextjs-nestjs-integration-example/9rwsssiyNm96i1CjjFpfhFSnSvGR
✅ Preview: https://nextjs-nestjs-git-fork-szabolcs-szilagyi-validation-pip-81bff7.vercel.app