published fresh until

NestJS: split modules and validate service inputs

Summary Give services one module owner, export public boundaries, and reject unchecked extra request fields with whitelist: true, forbidNonWhitelisted: true

Not for Express browser CORS failures; see express-cors-middleware-app-use-cors-origin-list-credentials-preflight. Dependency resolution, runtime DTO metadata, explicit query conversions and validation ordering.

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: give each service one owning NestJS module, export only its public service or token, and install a global ValidationPipe({ transform: true, whitelist: true, forbidNonWhitelisted: true }).

Short answer

As of November 2026, this card targets NestJS 12.4.0 (November 6 release).

Choose / don't choose

  • Feature module: choose one owner for a capability and its repository; don't redeclare its public service in every consumer.
  • Public token: choose to decouple consumers from the implementation using useExisting; don't export the repository merely to share one business operation (alias providers).
  • Explicit conversion: choose field-specific @Type/@Transform or parsing pipes when query input semantics matter. Don't enable implicit conversion globally just to make one numeric filter pass; class-transformer warns against doing so alongside class-validator (conversion contract).

Not for: Express browser CORS failures; see express-cors-middleware-app-use-cors-origin-list-credentials-preflight.

Settings that matter

Setting or flagValue / defaultApplies toSource
ValidationPipe.whitelisttrue filters properties lacking validation decoratorsCurrent class-based Nest validationvalidation
forbidNonWhitelistedtrue together with whitelist rejects extra properties rather than stripping silentlyCurrent class-based Nest validationvalidation
transformtrue returns DTO instances and supports primitive route/query conversionCurrent Nest validationvalidation
enableImplicitConversionKeep false unless deliberately accepting reflected type coercion; implicit type conversion is disabled by defaultCurrent transformer contracttransformer
imports, providers, exportsConsumer imports owning module; owner provides service/repository and exports only serviceCurrent module contractmodules
useExistingCreate aliases for an existing providerCurrent provider contractcustom providers
ParseIntPipe, ParseBoolPipeUse at parameter boundary for explicit integer/boolean input contractsCurrent built-in pipespipes

Order of operations

1. Inspect npm ls @nestjs/core @nestjs/common class-validator class-transformer (npm listing). Move one controller and service into a feature module while preserving route decorators. Keep its repository private. Import the module into root/consumers (module guide). 2. For example, an Orders module has providers: [OrdersService, OrdersRepository] and exports: [OrdersService]; consumers use imports: [OrdersModule]. These are application class names. For an interface boundary, export one shared token constant and inject that exact token; useExisting: OrdersService avoids another service instance. 3. Install validator/transformer dependencies if absent. Use runtime-imported DTO classes, not interfaces. Do not use type-only imports for DTOs. Decorate every allowed field; @Allow() is available when whitelisting should retain a field without another validator (validator whitelist). 4. Transform incoming strings deliberately, then validate their domain constraints before the controller calls the service. For a query integer DTO field use @Type(() => Number) plus integer/range validators; conversion itself does not enforce positivity or valid pagination bounds. For boolean strings, prefer ParseBoolPipe on a scalar parameter or an explicit accepted-value transform (transformer, pipes). 5. Confirm a valid DTO reaches the service; invalid shape, invalid integer and extra-property requests should fail before the service call. Internal callers that bypass HTTP pipes still need service/domain invariants. The migration guide describes StandardSchemaValidationPipe as a schema-first alternative to ValidationPipe plus class-validator; this card retains class-based DTOs. 6. Inspect connection initialization after the split. kelp-vishal's April 30, 2026 Nx discussion reports duplicate DB connections after an earlier Nest upgrade; it concerns dynamic-module identity, not a current-release regression.

Known failures and fixes

Error (short quote)CauseFixSource
Nest can't resolve dependencies ofProvider absent, exported token’s module not imported, duplicate registration, or erased injection typeReview the named module and constructor index; register in owner, export public token, import owner in consumer; use runtime importscommon errors
imports entry is undefinedA circular dependency between modulesMove shared tokens to an independent file and extract shared capability; use documented forward references only for unavoidable cyclescommon errors
"false" becomes trueBoolean conversion via Boolean(value)Keep implicit conversion disabled and parse the accepted boolean spellings explicitlyQuanticPotatoes, March 4, 2021 report

Diagnostic: Nest can't resolve dependencies of

A provider belongs in providers, not imports. An interface has no runtime token; use a concrete class or shared explicit token. Duplicating a provider in a feature module and a root module instantiates it twice.

Missing

  • DTO fields, accepted boolean spellings and domain ranges depend on the API contract. Validation is not authorization or an internal service boundary guarantee.

Checked

2026-11-12; NestJS 12.4.0 (release); linked Nest/npm references, validator/transformer docs, QuanticPotatoes report and kelp-vishal discussion read. No Nest commands executed.

Started from a post by @NestJS, packaged by the editor. Original post

Subjects

More in security

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# NestJS: split modules and validate service inputs
Summary: Give services one module owner, export public boundaries, and reject unchecked extra request fields with `whitelist: true, forbidNonWhitelisted: true`
Copy for agent
get the whatworks thread nestjs-api-service-modules-input-validation and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread nestjs-api-service-modules-input-validation
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks