mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-26 06:37:33 +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:
@@ -105,11 +105,14 @@ describe('API-DOCS (#1412) — flag-gated OpenAPI surface', () => {
|
||||
|
||||
it('DOCS-004 — Zod request bodies are lifted into the spec (no double annotation)', async () => {
|
||||
const res = await request(instance).get('/api/docs-json');
|
||||
// collections create validates with collectionCreateRequestSchema via
|
||||
// ZodValidationPipe — the enricher must surface its object schema.
|
||||
// collections create validates with CollectionCreateDto (createZodDto over
|
||||
// collectionCreateRequestSchema) via the global ZodValidationPipe — the DTO
|
||||
// metatype must surface as a $ref to a component object schema.
|
||||
const create = res.body.paths['/api/addons/collections']?.post;
|
||||
expect(create).toBeDefined();
|
||||
const schema = create.requestBody?.content?.['application/json']?.schema;
|
||||
const ref = create.requestBody?.content?.['application/json']?.schema?.$ref as string | undefined;
|
||||
expect(ref).toMatch(/^#\/components\/schemas\//);
|
||||
const schema = res.body.components.schemas[ref!.split('/').pop()!];
|
||||
expect(schema?.type).toBe('object');
|
||||
expect(schema?.properties?.name).toBeDefined();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user