mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 08:30:05 +00:00
feat(server): adopt nestjs-zod with a fail-closed body-contract boot gate
- One global ZodValidationPipe (APP_PIPE, createZodValidationPipe) validates
any @Body() typed with a createZodDto class; the custom exception factory
keeps TREK's { error: 'field: message; ...' } envelope byte-identical.
- The 7 piped controllers (collections, places, vacay, todo, health,
airtrail x2) now type bodies with colocated <domain>.dto.ts wrappers —
the Zod schemas in @trek/shared stay the single source of truth.
- OpenAPI: the hand-rolled api-zod.ts enricher is replaced by nestjs-zod's
cleanupOpenApiDoc; request bodies surface as $refs to named DTO schemas.
- validateBodyContracts() runs in buildApp() after init and refuses to boot
when any POST/PUT/PATCH @Body() (including @Body('field') sub-reads) lacks
a ZodDto metatype. The 175 pre-existing legacy handlers are grandfathered
in body-contract-allow-list.ts; the list is ratchet-only — the gate also
throws on stale entries, so migrating a domain forces their removal.
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import type { INestApplication } from '@nestjs/common';
|
||||
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
|
||||
import { cleanupOpenApiDoc } from 'nestjs-zod';
|
||||
import { readEnv } from '../../app-config';
|
||||
import { attachZodBodySchemas } from '../common/api-zod';
|
||||
|
||||
/**
|
||||
* Swagger UI + OpenAPI spec for the REST API (#1412), gated behind
|
||||
@@ -29,10 +29,12 @@ export function setupApiDocs(app: INestApplication): void {
|
||||
.addCookieAuth('trek_session')
|
||||
.addSecurityRequirements('session')
|
||||
.build();
|
||||
const document = SwaggerModule.createDocument(app, config);
|
||||
// Lift the Zod schemas the routes already validate with into the document —
|
||||
// no double annotation, every ZodValidationPipe body is documented.
|
||||
attachZodBodySchemas(app, document);
|
||||
const rawDocument = SwaggerModule.createDocument(app, config);
|
||||
// The createZodDto classes on @Body() params carry their @trek/shared Zod
|
||||
// schemas into the document; cleanupOpenApiDoc (nestjs-zod) normalizes the
|
||||
// zod-generated JSON Schema output — no double annotation, every validated
|
||||
// body is documented.
|
||||
const document = cleanupOpenApiDoc(rawDocument);
|
||||
SwaggerModule.setup('api/docs', app, document, {
|
||||
jsonDocumentUrl: 'api/docs-json',
|
||||
yamlDocumentUrl: 'api/docs-yaml',
|
||||
|
||||
Reference in New Issue
Block a user