skip to content

As lead of a NestJS 12 codebase built on class-validator DTOs, how would you decide whether to adopt StandardSchemaValidationPipe, and how would you migrate?

level: principalimportance: should knowfreq 25%

answer

  1. nothing forces the move
  2. one source of truth either way
  3. where unknown-key policy lives
  4. both pipes can run together
  5. error wording changes for clients

basics

~20 s

Nest 12 keeps ValidationPipe fully supported, so adopting StandardSchemaValidationPipe is a choice, not an upgrade task. Weigh schema-inferred types, coercion and arrays against DTO classes' mapped types and Swagger plugin, then register both pipes and migrate endpoint by endpoint.

solid answer

~50 s

Nothing forces the move: the Nest 12 migration guide says the decorator-based approach remains fully supported, with no plans to remove it. Schemas earn their place where DTO classes hurt: the TypeScript type is inferred from the schema, coercion and defaults live in it, arrays need no `ParseArrayPipe`, and no reflection metadata is involved, so interfaces and `import type` stop being traps. DTO classes keep the mapped-types utilities and the Swagger CLI plugin integration a large codebase may lean on. If you adopt, register both pipes globally; each validates only its own kind of parameter, so endpoints move one at a time. Decide up front where unknown-key policy lives, because `whitelist` does not apply to schema parameters and each schema chooses to strip or reject, and give both pipes one `exceptionFactory` shape, since message wording now comes from the schema library.

go deeper

for a junior

Recall that Nest 12 supports both class-validator DTOs with ValidationPipe and schemas with StandardSchemaValidationPipe, and that neither replaces the other.

for a middle

Explain how each pipe picks the parameters it validates, and what moves from pipe options into the schema when an endpoint switches.

for a senior

Show how you would run both pipes without double validation, keep the error contract stable, and test unknown fields and coercion per endpoint.

for a principal

Own the call: what a second validation style costs reviewers and clients, which endpoints justify it, and which conventions must be fixed before anyone migrates.

## What Nest 12 actually changed Nest 12 added a `schema` option to parameter decorators and a built-in `StandardSchemaValidationPipe` that enforces it with any library implementing the **Standard Schema** interface. The migration guide frames this as an addition: the decorator-based `ValidationPipe` with class-validator remains fully supported with no plans to remove it, and the guide's own advice is to use `ValidationPipe` when DTOs are class-based and the schema pipe when the validation library already exposes Standard Schema-compatible schemas. So the lead's question is not "must we migrate?" but "does a second style pay for itself here?" ## What each approach gives a team | Concern | DTO classes + `ValidationPipe` | Schemas + `StandardSchemaValidationPipe` | |---|---|---| | Where rules live | Decorators on a class that is also the TypeScript type | A schema object; the type is inferred from it | | Runtime needs | class-validator, class-transformer, emitted type metadata, concrete classes | The schema library; no reflection metadata | | Numeric strings in a body | `@Type(() => Number)` or implicit conversion | Coercion declared in the schema | | Array bodies | `ParseArrayPipe` or a wrapper class | The array schema validates each element | | Unknown keys | One global `whitelist` / `forbidNonWhitelisted` setting | Chosen per schema (strip, reject or keep) | | Derived variants | `PartialType`, `PickType`, `OmitType`, `IntersectionType` | The library's own composition methods | | Error messages | class-validator wording, such as `quantity must not be less than 1` | Library wording prefixed with a path, such as `quantity: Too small: expected number to be >=1` | | API docs | Integrates with the Swagger CLI plugin | The same schemas can feed OpenAPI generation | ## When staying put is the right call - The codebase leans on the mapped-types helpers and the Swagger plugin across hundreds of DTOs, and those work. - Clients parse validation `message` strings, and changing their wording would be a breaking change you cannot coordinate. - The team's real pain is elsewhere; a second validation style doubles what reviewers must know. ## When adopting pays - New services or modules where no DTOs exist yet. - Endpoints repeatedly bitten by the class approach: numeric strings, nested arrays, interfaces or `import type` silently skipped. - A team already using one Standard Schema library for configuration validation, which `@nestjs/config` also accepts in Nest 12, and wanting one schema language across both. ## A migration plan 1. **Register both pipes globally**: `app.useGlobalPipes(new ValidationPipe({ ... }), new StandardSchemaValidationPipe())`. `ValidationPipe` validates only class-typed parameters and the schema pipe only parameters that declare a schema, so untouched endpoints keep working. 2. **Keep each parameter on one mechanism.** Type a schema-validated parameter with the inferred type, not with a DTO class; a class-typed parameter carrying a schema would be checked by both pipes. 3. **Fix the error contract first.** Give both pipes an `exceptionFactory` that emits the same body shape and status, so a client cannot tell which pipe answered. 4. **Write down the unknown-key convention**, for instance strict objects for write endpoints if `forbidNonWhitelisted` was the policy, and enforce it in review. 5. **Migrate by value**: new endpoints first, then the ones the class approach hurts; leave stable ones alone. 6. **Pin behaviour with tests** that send unknown fields, numeric strings and malformed items, and assert the status and body. ## The costs the team carries - Two validation styles side by side for as long as the migration lasts, possibly for good. - Message wording and unknown-key behaviour can shift per endpoint, which clients notice. - Schema `transform` defaults to returning the schema's output, so coercion or defaults that used to happen in services may now happen at the boundary; review for double conversion.

  • What can break for API clients when an endpoint moves from ValidationPipe to StandardSchemaValidationPipe?
    Message wording changes from class-validator's, such as `quantity must not be less than 1`, to the schema library's path-prefixed messages. Unknown keys follow the schema's policy instead of the global `whitelist` setting, and numeric strings may now be coerced or rejected differently. A shared `exceptionFactory` and contract tests keep the change invisible.
  • How do you stop both global pipes from validating the same parameter?
    Rely on how each pipe chooses its work: `ValidationPipe` validates when the declared type is a class, and `StandardSchemaValidationPipe` when the parameter declares a schema. Type schema parameters with the schema's inferred type, a type alias that reports `Object`, and never with a DTO class, so exactly one pipe applies.

  • Nest 12 deprecates ValidationPipe, so every DTO must become a schema
  • The two validation pipes cannot both be registered globally
  • ValidationPipe's whitelist still strips unknown keys on schema-validated parameters
  • Switching pipes leaves validation messages unchanged for clients

Assumes NestJS 12, the first major with the schema option and StandardSchemaValidationPipe; its migration guide keeps ValidationPipe fully supported.