nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
66.9k stars 7.55k forks source link

ValidationPipe validates nullable Args set to null #12736

Closed nsams closed 10 months ago

nsams commented 10 months ago

Is there an existing issue for this?

Current behavior

ValidationPipe validates nullable Args set to null, resulting in a validation error for nullable arg.

Minimum reproduction code

https://stackblitz.com/edit/nestjs-typescript-starter-f5fhvk?file=src%2Fmain.ts,package.json,src%2Fapp.module.ts,src%2Fapp.resolver.ts&terminal=start

Steps to reproduce

  1. open provided stackblitz
  2. run the following query query { sayHellow }
  3. you get a validation error ("firstName must be a string")

Expected behavior

As the Arg is nullable, no validation should be done.

Package

Other package

No response

NestJS version

10.2.6

Packages versions

[System Information] OS Version : Linux 5.0 NodeJS Version : v18.18.0 NPM Version : 9.4.2

[Nest CLI] Nest CLI Version : 10.1.18

[Nest Platform Information] platform-express version : 10.2.6 schematics version : 10.0.2 graphql version : 12.0.10 testing version : 10.2.6 apollo version : 12.0.10 common version : 10.2.6 core version : 10.2.6 cli version : 10.1.18

Node.js version

18.18.0

In which operating systems have you tested?

Other

Related Issue: #3930 and commit 233bc860e728b706cfae1858bd601288f237befc: that takes care about not transforming null. My issue is about validating it.

jmcdo29 commented 10 months ago

The ValidationPipe (class-validator) is not aware of what metadata the decorators from @nestjs/graphql are setting. So you need to tell both systems (graphql and class-validator) that the field is nullable, not just graphql. For further assistance, please use our Discord as this is not a bug