zod-nest 0.4.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +73 -2
- package/dist/index.d.ts +73 -2
- package/dist/index.js +487 -11
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +483 -13
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -1
package/dist/index.d.mts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
2
|
import { JSONSchema, $ZodTypes } from 'zod/v4/core';
|
|
3
|
-
import { InternalServerErrorException, ExecutionContext, BadRequestException, ArgumentMetadata, LoggerService, PipeTransform, NestInterceptor, CallHandler, DynamicModule } from '@nestjs/common';
|
|
3
|
+
import { InternalServerErrorException, ExecutionContext, BadRequestException, ArgumentMetadata, LoggerService, PipeTransform, NestInterceptor, CallHandler, DynamicModule, INestApplication } from '@nestjs/common';
|
|
4
4
|
import { Reflector } from '@nestjs/core';
|
|
5
5
|
import { Observable } from 'rxjs';
|
|
6
|
+
import { OpenAPIObject } from '@nestjs/swagger';
|
|
6
7
|
|
|
7
8
|
/** OpenAPI 3.1 `$ref` prefix for entries in `components.schemas`. */
|
|
8
9
|
declare const COMPONENTS_SCHEMAS_PREFIX = "#/components/schemas/";
|
|
@@ -32,6 +33,13 @@ interface ZodNestRegistry {
|
|
|
32
33
|
register(schema: z.ZodType, id: string): void;
|
|
33
34
|
hasCollision(id: string): boolean;
|
|
34
35
|
getCollisions(): ReadonlyMap<string, ReadonlySet<z.ZodType>>;
|
|
36
|
+
/**
|
|
37
|
+
* Snapshot of every id registered through this `ZodNestRegistry`. Phase 2e's
|
|
38
|
+
* bulk-mode emitter uses it to filter `z.toJSONSchema(registry.zodRegistry,
|
|
39
|
+
* ...)` output to zod-nest-known ids only (the underlying Zod registry is
|
|
40
|
+
* `z.globalRegistry`, which can hold third-party entries).
|
|
41
|
+
*/
|
|
42
|
+
ids(): readonly string[];
|
|
35
43
|
}
|
|
36
44
|
declare const createRegistry: () => ZodNestRegistry;
|
|
37
45
|
/**
|
|
@@ -399,4 +407,67 @@ declare class ZodNestModule {
|
|
|
399
407
|
static forRoot(options?: ZodNestModuleOptions): DynamicModule;
|
|
400
408
|
}
|
|
401
409
|
|
|
402
|
-
|
|
410
|
+
interface ApplyZodNestOptions {
|
|
411
|
+
/**
|
|
412
|
+
* The NestJS app instance. Required so `applyZodNest` can walk controllers
|
|
413
|
+
* via `DiscoveryService` to pick up `@ZodResponse` output-side DTO usage —
|
|
414
|
+
* `@nestjs/swagger` is currently anemic on response shapes.
|
|
415
|
+
*/
|
|
416
|
+
app: INestApplication;
|
|
417
|
+
/**
|
|
418
|
+
* `ZodNestRegistry` instance that holds the zod-nest DTOs. Defaults to
|
|
419
|
+
* `defaultRegistry` (the process-wide singleton populated by `createZodDto`).
|
|
420
|
+
* Pass an explicit registry for multi-app isolation (planned formalization
|
|
421
|
+
* in v0.2).
|
|
422
|
+
*/
|
|
423
|
+
registry?: ZodNestRegistry;
|
|
424
|
+
/** User override pipe applied on top of the built-in override during emission. */
|
|
425
|
+
override?: Override;
|
|
426
|
+
/**
|
|
427
|
+
* Strict mode (default `true`) throws `ZodNestUnrepresentableError` on
|
|
428
|
+
* unrepresentable Zod constructs (bigint / date / symbol / transform / ...).
|
|
429
|
+
* Set to `false` to emit `{}` for those instead.
|
|
430
|
+
*/
|
|
431
|
+
strict?: boolean;
|
|
432
|
+
}
|
|
433
|
+
/**
|
|
434
|
+
* Post-processor over the OpenAPI document emitted by
|
|
435
|
+
* `SwaggerModule.createDocument`. Mutates the doc in place AND returns it for
|
|
436
|
+
* compositional convenience. After this runs:
|
|
437
|
+
*
|
|
438
|
+
* - Every `components.schemas[<DtoClassName>]` placeholder with an
|
|
439
|
+
* `x-zod-nest-dto` marker is replaced by the Zod-derived JSON Schema body,
|
|
440
|
+
* keyed by the marker's `dtoId` (renaming as needed).
|
|
441
|
+
* - The I/O suffix truth table is applied — equal input/output bodies collapse
|
|
442
|
+
* to one `components.schemas[id]`; divergent bodies split as
|
|
443
|
+
* `id` (input) + `idOutput` (output), with response-side refs rewritten.
|
|
444
|
+
* - Every `$ref` whose target is missing throws `ZodNestDocumentError(DANGLING_REF)`.
|
|
445
|
+
*
|
|
446
|
+
* Composable with other doc-transform passes — apply other mutations before
|
|
447
|
+
* or after this function. The `app` argument is required because the
|
|
448
|
+
* output-side DTO usage lives on controller-method metadata that the doc
|
|
449
|
+
* doesn't surface; `DiscoveryService` resolves it.
|
|
450
|
+
*/
|
|
451
|
+
declare const applyZodNest: (doc: OpenAPIObject, opts: ApplyZodNestOptions) => OpenAPIObject;
|
|
452
|
+
|
|
453
|
+
type ZodNestDocumentErrorCode = 'AMBIGUOUS_RENAME' | 'DANGLING_REF';
|
|
454
|
+
/**
|
|
455
|
+
* Thrown by `applyZodNest` when the doc cannot be processed cleanly. Surfaces
|
|
456
|
+
* at doc-build time so typos / mis-registrations fail in CI, not at runtime.
|
|
457
|
+
*
|
|
458
|
+
* `AMBIGUOUS_RENAME`: two distinct DTO classes target the same registry id
|
|
459
|
+
* with differing bodies — the rename pass can't write `components.schemas[id]`
|
|
460
|
+
* unambiguously.
|
|
461
|
+
*
|
|
462
|
+
* `DANGLING_REF`: a `$ref` in the doc points at a `components.schemas` key
|
|
463
|
+
* that no longer exists after `applyZodNest`. Usually means a marker was
|
|
464
|
+
* stripped but its rename target wasn't populated, or a user-supplied pre-pass
|
|
465
|
+
* left a stale ref.
|
|
466
|
+
*/
|
|
467
|
+
declare class ZodNestDocumentError extends ZodNestError {
|
|
468
|
+
readonly code: ZodNestDocumentErrorCode;
|
|
469
|
+
readonly details: Readonly<Record<string, unknown>>;
|
|
470
|
+
constructor(code: ZodNestDocumentErrorCode, message: string, details?: Record<string, unknown>);
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
export { type ApplyZodNestOptions, COMPONENTS_SCHEMAS_PREFIX, type CreateSerializationException, type CreateValidationException, type CreateZodDtoOptions, DEFAULT_MAX_LOGGED_VALUE_BYTES, DEFAULT_REDACT_KEYS, type Io, type NormalizedZodNestOptions, type Override, type OverrideContext, type ResponseVariant, type ResponseVariantKind, type SchemaObject, type ToOpenApiOptions, type ToOpenApiResult, ZOD_DTO_SYMBOL, ZOD_NEST_DTO_EXTENSION, ZOD_NEST_ERROR_DUPLICATE_ID, ZOD_NEST_ERROR_EXTENSION, ZOD_NEST_OPTIONS, ZOD_RESPONSES_METADATA_KEY, type ZodDto, type ZodDtoMarker, ZodNestDocumentError, type ZodNestDocumentErrorCode, ZodNestError, ZodNestModule, type ZodNestModuleOptions, type ZodNestRegistry, ZodNestUnrepresentableError, ZodResponse, type ZodResponseDescription, type ZodResponseOptions, type ZodResponseType, ZodSerializationException, ZodSerializerInterceptor, ZodValidationException, ZodValidationPipe, type ZodValidationPipeArg, type ZodValidationPipeOptions, applyZodNest, createRegistry, createZodDto, defaultRegistry, defaultStatusFor, isZodDto, isZodDtoMarker, makeZodDtoMarker, resolveEffectiveStatus, toOpenApi };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
2
|
import { JSONSchema, $ZodTypes } from 'zod/v4/core';
|
|
3
|
-
import { InternalServerErrorException, ExecutionContext, BadRequestException, ArgumentMetadata, LoggerService, PipeTransform, NestInterceptor, CallHandler, DynamicModule } from '@nestjs/common';
|
|
3
|
+
import { InternalServerErrorException, ExecutionContext, BadRequestException, ArgumentMetadata, LoggerService, PipeTransform, NestInterceptor, CallHandler, DynamicModule, INestApplication } from '@nestjs/common';
|
|
4
4
|
import { Reflector } from '@nestjs/core';
|
|
5
5
|
import { Observable } from 'rxjs';
|
|
6
|
+
import { OpenAPIObject } from '@nestjs/swagger';
|
|
6
7
|
|
|
7
8
|
/** OpenAPI 3.1 `$ref` prefix for entries in `components.schemas`. */
|
|
8
9
|
declare const COMPONENTS_SCHEMAS_PREFIX = "#/components/schemas/";
|
|
@@ -32,6 +33,13 @@ interface ZodNestRegistry {
|
|
|
32
33
|
register(schema: z.ZodType, id: string): void;
|
|
33
34
|
hasCollision(id: string): boolean;
|
|
34
35
|
getCollisions(): ReadonlyMap<string, ReadonlySet<z.ZodType>>;
|
|
36
|
+
/**
|
|
37
|
+
* Snapshot of every id registered through this `ZodNestRegistry`. Phase 2e's
|
|
38
|
+
* bulk-mode emitter uses it to filter `z.toJSONSchema(registry.zodRegistry,
|
|
39
|
+
* ...)` output to zod-nest-known ids only (the underlying Zod registry is
|
|
40
|
+
* `z.globalRegistry`, which can hold third-party entries).
|
|
41
|
+
*/
|
|
42
|
+
ids(): readonly string[];
|
|
35
43
|
}
|
|
36
44
|
declare const createRegistry: () => ZodNestRegistry;
|
|
37
45
|
/**
|
|
@@ -399,4 +407,67 @@ declare class ZodNestModule {
|
|
|
399
407
|
static forRoot(options?: ZodNestModuleOptions): DynamicModule;
|
|
400
408
|
}
|
|
401
409
|
|
|
402
|
-
|
|
410
|
+
interface ApplyZodNestOptions {
|
|
411
|
+
/**
|
|
412
|
+
* The NestJS app instance. Required so `applyZodNest` can walk controllers
|
|
413
|
+
* via `DiscoveryService` to pick up `@ZodResponse` output-side DTO usage —
|
|
414
|
+
* `@nestjs/swagger` is currently anemic on response shapes.
|
|
415
|
+
*/
|
|
416
|
+
app: INestApplication;
|
|
417
|
+
/**
|
|
418
|
+
* `ZodNestRegistry` instance that holds the zod-nest DTOs. Defaults to
|
|
419
|
+
* `defaultRegistry` (the process-wide singleton populated by `createZodDto`).
|
|
420
|
+
* Pass an explicit registry for multi-app isolation (planned formalization
|
|
421
|
+
* in v0.2).
|
|
422
|
+
*/
|
|
423
|
+
registry?: ZodNestRegistry;
|
|
424
|
+
/** User override pipe applied on top of the built-in override during emission. */
|
|
425
|
+
override?: Override;
|
|
426
|
+
/**
|
|
427
|
+
* Strict mode (default `true`) throws `ZodNestUnrepresentableError` on
|
|
428
|
+
* unrepresentable Zod constructs (bigint / date / symbol / transform / ...).
|
|
429
|
+
* Set to `false` to emit `{}` for those instead.
|
|
430
|
+
*/
|
|
431
|
+
strict?: boolean;
|
|
432
|
+
}
|
|
433
|
+
/**
|
|
434
|
+
* Post-processor over the OpenAPI document emitted by
|
|
435
|
+
* `SwaggerModule.createDocument`. Mutates the doc in place AND returns it for
|
|
436
|
+
* compositional convenience. After this runs:
|
|
437
|
+
*
|
|
438
|
+
* - Every `components.schemas[<DtoClassName>]` placeholder with an
|
|
439
|
+
* `x-zod-nest-dto` marker is replaced by the Zod-derived JSON Schema body,
|
|
440
|
+
* keyed by the marker's `dtoId` (renaming as needed).
|
|
441
|
+
* - The I/O suffix truth table is applied — equal input/output bodies collapse
|
|
442
|
+
* to one `components.schemas[id]`; divergent bodies split as
|
|
443
|
+
* `id` (input) + `idOutput` (output), with response-side refs rewritten.
|
|
444
|
+
* - Every `$ref` whose target is missing throws `ZodNestDocumentError(DANGLING_REF)`.
|
|
445
|
+
*
|
|
446
|
+
* Composable with other doc-transform passes — apply other mutations before
|
|
447
|
+
* or after this function. The `app` argument is required because the
|
|
448
|
+
* output-side DTO usage lives on controller-method metadata that the doc
|
|
449
|
+
* doesn't surface; `DiscoveryService` resolves it.
|
|
450
|
+
*/
|
|
451
|
+
declare const applyZodNest: (doc: OpenAPIObject, opts: ApplyZodNestOptions) => OpenAPIObject;
|
|
452
|
+
|
|
453
|
+
type ZodNestDocumentErrorCode = 'AMBIGUOUS_RENAME' | 'DANGLING_REF';
|
|
454
|
+
/**
|
|
455
|
+
* Thrown by `applyZodNest` when the doc cannot be processed cleanly. Surfaces
|
|
456
|
+
* at doc-build time so typos / mis-registrations fail in CI, not at runtime.
|
|
457
|
+
*
|
|
458
|
+
* `AMBIGUOUS_RENAME`: two distinct DTO classes target the same registry id
|
|
459
|
+
* with differing bodies — the rename pass can't write `components.schemas[id]`
|
|
460
|
+
* unambiguously.
|
|
461
|
+
*
|
|
462
|
+
* `DANGLING_REF`: a `$ref` in the doc points at a `components.schemas` key
|
|
463
|
+
* that no longer exists after `applyZodNest`. Usually means a marker was
|
|
464
|
+
* stripped but its rename target wasn't populated, or a user-supplied pre-pass
|
|
465
|
+
* left a stale ref.
|
|
466
|
+
*/
|
|
467
|
+
declare class ZodNestDocumentError extends ZodNestError {
|
|
468
|
+
readonly code: ZodNestDocumentErrorCode;
|
|
469
|
+
readonly details: Readonly<Record<string, unknown>>;
|
|
470
|
+
constructor(code: ZodNestDocumentErrorCode, message: string, details?: Record<string, unknown>);
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
export { type ApplyZodNestOptions, COMPONENTS_SCHEMAS_PREFIX, type CreateSerializationException, type CreateValidationException, type CreateZodDtoOptions, DEFAULT_MAX_LOGGED_VALUE_BYTES, DEFAULT_REDACT_KEYS, type Io, type NormalizedZodNestOptions, type Override, type OverrideContext, type ResponseVariant, type ResponseVariantKind, type SchemaObject, type ToOpenApiOptions, type ToOpenApiResult, ZOD_DTO_SYMBOL, ZOD_NEST_DTO_EXTENSION, ZOD_NEST_ERROR_DUPLICATE_ID, ZOD_NEST_ERROR_EXTENSION, ZOD_NEST_OPTIONS, ZOD_RESPONSES_METADATA_KEY, type ZodDto, type ZodDtoMarker, ZodNestDocumentError, type ZodNestDocumentErrorCode, ZodNestError, ZodNestModule, type ZodNestModuleOptions, type ZodNestRegistry, ZodNestUnrepresentableError, ZodResponse, type ZodResponseDescription, type ZodResponseOptions, type ZodResponseType, ZodSerializationException, ZodSerializerInterceptor, ZodValidationException, ZodValidationPipe, type ZodValidationPipeArg, type ZodValidationPipeOptions, applyZodNest, createRegistry, createZodDto, defaultRegistry, defaultStatusFor, isZodDto, isZodDtoMarker, makeZodDtoMarker, resolveEffectiveStatus, toOpenApi };
|