ValidationPipe whitelist default shift in NestJS 9 and the open question on forbidUnknownValues
0 reputation · 07 Jan 2024, 03:50 UTC
0 reputation · 07 Jan 2024, 03:50 UTC
The shift in NestJS 9 changed the ValidationPipe’s default whitelist option from false to true, causing unknown properties in DTO payloads to be stripped silently. Teams upgrading from earlier versions notice missing data that previously flowed through to controllers, which can hide bugs in business logic. The goal is to determine whether the framework should also change the default value of forbidUnknownValues to true so that unexpected fields trigger an explicit exception instead of being ignored.
Any change must balance backward compatibility for existing applications that rely on the current silent‑strip behavior against the desire for stricter input validation that surfaces data‑integrity problems early. The NestJS team has noted the shift in release notes, yet the community remains divided on whether forbidUnknownValues should become the new default, leaving the decision unresolved.
Should the default forbidUnknownValues be set to true in NestJS 9+ to enforce strict validation of DTOs? What migration strategy would allow projects to adopt the new default without breaking existing controllers that depend on the current silent‑strip behavior?
The framework default for forbidUnknownValues should remain false in NestJS 9 and later. Changing it to true globally would convert previously accepted payloads with extra fields into 400 validation errors, breaking existing clients, proxies, and test suites without code changes. Teams that want strict rejection should enable it explicitly on a per-pipe or per-route basis.
BadRequestException listing the unknown keys.falseMaking strict rejection the default would surface contract drift early, but it also rejects harmless metadata that gateways, CDNs, or older mobile clients routinely append (request IDs, tracing headers, feature flags). In a mature codebase, those extra fields are often ignored by business logic; turning them into hard errors forces a coordinated client-and-server deploy or a temporary rollback. The NestJS team documented the whitelist default shift in release notes precisely because it was a non-breaking change—silent stripping preserves response shape. A forbidUnknownValues default flip would be breaking.
forbidUnknownValues: false (or omit it) in the app-wide ValidationPipe registration.forbidUnknownValues: true and attach it via @UsePipes() on new controllers or feature modules. This surfaces unknown fields during review without affecting production traffic.What is the exact @nestjs/common and class-validator version pair in the production deployment? The whitelist default changed in NestJS 9.0.0, but class-validator 0.14+ altered error message formatting; knowing the versions confirms whether the current silent-strip behavior matches the documented baseline.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.