zod-nest 0.13.0 → 1.1.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/README.md CHANGED
@@ -431,7 +431,7 @@ A compact, link-out index. Type signatures and detailed semantics live in the co
431
431
  - `ZodNestModule.forRoot(options?)`, `ZodNestModuleOptions`, `DEFAULT_REDACT_KEYS`, `DEFAULT_MAX_LOGGED_VALUE_BYTES`, `ZOD_NEST_OPTIONS`
432
432
 
433
433
  **Schema engine** — single-schema mode and extension points
434
- - `toOpenApi(schema, opts)`, `createRegistry()`, `defaultRegistry`, `ZodNestRegistry`, `Override`, `OverrideContext`, `overrideJSONSchema(schema, fragment)`, `ZodNestError`, `ZodNestUnrepresentableError`, `extend`, `getLineage`, `LineageEntry`
434
+ - `toOpenApi(schema, opts)`, `createRegistry()`, `defaultRegistry`, `ZodNestRegistry`, `Override`, `OverrideContext`, `overrideJSONSchema(schema, fragment | { input?, output? })`, `OverrideJSONSchemaArg`, `ZodNestError`, `ZodNestUnrepresentableError`, `extend`, `getLineage`, `LineageEntry`
435
435
 
436
436
  ## Documentation
437
437
 
@@ -450,16 +450,18 @@ A compact, link-out index. Type signatures and detailed semantics live in the co
450
450
 
451
451
  ## Compatibility matrix
452
452
 
453
- `zod-nest` declares broad peer-dep ranges in `package.json`: `zod >= 4.4.0`, `@nestjs/common >= 10`, `@nestjs/core >= 10`, `@nestjs/swagger >= 8`, `rxjs >= 7`, Node `>= 22`. CI validates those claims by running the full test suite against the cells below; a red cell is a real blocker.
453
+ `zod-nest` declares explicit min + max peer-dep ranges in `package.json`: `zod >=4.4.0 <5.0.0`, `@nestjs/common >=11.0.1 <12.0.0`, `@nestjs/core >=11.0.1 <12.0.0`, `@nestjs/swagger >=11.0.0 <12.0.0`, `rxjs >=7.1.0 <8.0.0`, `reflect-metadata >=0.2.0 <0.3.0`, Node `>=22`. CI validates those claims by running the full test suite against the cells below; a red cell is a real blocker. Upper bounds are deliberate — a new peer major has to land in a real PR with a `/check-upstream-updates` audit before consumers can install it against this library.
454
454
 
455
- | Cell | `zod` | `@nestjs/common` | `@nestjs/core` | `@nestjs/swagger` |
456
- |---|---|---|---|---|
457
- | `floor` | 4.4.0 | 10.0.0 | 10.0.0 | 8.0.0 |
458
- | `zod-latest` | latest | 10.0.0 | 10.0.0 | 8.0.0 |
459
- | `nest-latest` | 4.4.0 | latest | latest | latest |
460
- | `all-latest` | latest | latest | latest | latest |
455
+ | Cell | `zod` | `@nestjs/common` | `@nestjs/core` | `@nestjs/swagger` | `rxjs` | `reflect-metadata` |
456
+ |---|---|---|---|---|---|---|
457
+ | `floor` | 4.4.0 | 11.0.1 | 11.0.1 | 11.0.0 | 7.1.0 | 0.2.0 |
458
+ | `zod-latest` | latest | 11.0.1 | 11.0.1 | 11.0.0 | 7.1.0 | 0.2.0 |
459
+ | `nest-latest` | 4.4.0 | latest | latest | latest | 7.1.0 | 0.2.0 |
460
+ | `rxjs-latest` | 4.4.0 | 11.0.1 | 11.0.1 | 11.0.0 | latest | 0.2.0 |
461
+ | `reflect-metadata-latest` | 4.4.0 | 11.0.1 | 11.0.1 | 11.0.0 | 7.1.0 | latest |
462
+ | `all-latest` | latest | latest | latest | latest | latest | latest |
461
463
 
462
- Cell definitions live in [`.github/compat-matrix.json`](.github/compat-matrix.json). The CI workflow ([`.github/workflows/compat-matrix.yml`](.github/workflows/compat-matrix.yml)) runs on every push to `main` and weekly on Monday — when a cell fails, the workflow opens (or comments on) a GitHub issue labelled `compat-matrix-failure` so the regression is tracked outside the Actions UI. Editing the JSON is the formal way to extend or shrink supported ranges Node + rxjs are not currently matrixed (Node floor `22` is enforced by `engines`; rxjs has been stable enough not to need cells yet).
464
+ Cell definitions live in [`.github/compat-matrix.json`](.github/compat-matrix.json). The CI workflow ([`.github/workflows/compat-matrix.yml`](.github/workflows/compat-matrix.yml)) runs on every push to `main` and weekly on Monday — when a cell fails, the workflow opens (or comments on) a GitHub issue labelled `compat-matrix-failure` so the regression is tracked outside the Actions UI. Editing the JSON is the formal way to extend or shrink supported ranges. Node is not matrixed the `>=22` floor is enforced by `engines`.
463
465
 
464
466
  ## Migration from `nestjs-zod`
465
467
 
package/dist/index.d.mts CHANGED
@@ -79,27 +79,50 @@ declare const extend: <P extends z.ZodObject, S extends z.ZodObject>(parent: P,
79
79
  */
80
80
  declare const getLineage: (schema: z.ZodType) => LineageEntry | undefined;
81
81
 
82
+ /**
83
+ * Argument shape for {@link overrideJSONSchema}. Two forms:
84
+ *
85
+ * - A raw `SchemaObject` — applied to both input and output emission.
86
+ * - A `{ input?, output? }` wrapper — distinct fragments per emission side,
87
+ * for coercion shapes where input and output diverge (e.g.
88
+ * `z.union([z.array(item), item.transform((v) => [v])])` accepts
89
+ * `T | T[]` on input but always emits `T[]` on output).
90
+ *
91
+ * The wrapper form is detected by the presence of an `input` or `output`
92
+ * key. Neither is a JSON Schema / OpenAPI 3.1 keyword, so the discriminator
93
+ * is unambiguous in practice.
94
+ */
95
+ type OverrideJSONSchemaArg = SchemaObject | {
96
+ input?: SchemaObject;
97
+ output?: SchemaObject;
98
+ };
82
99
  /**
83
100
  * Register a fixed JSON Schema fragment for a specific Zod schema instance.
84
101
  *
85
102
  * Designed for shapes JSON Schema can't model directly — `z.custom<T>()` and
86
103
  * `z.instanceof(...)` (e.g. multipart `File` fields) — which Zod emits as `{}`,
87
- * tripping `ZodNestUnrepresentableError` in strict mode. After registration
88
- * the engine writes the supplied fragment in-place wherever that schema
89
- * instance is emitted (single-schema `toOpenApi`, bulk emission, nested
90
- * inside `z.object({...})`, anywhere).
104
+ * tripping `ZodNestUnrepresentableError` in strict mode. Also useful for
105
+ * coercion shapes where input and output diverge (`singleOrArray`-style
106
+ * helpers).
91
107
  *
92
- * Idempotent: subsequent calls for the same schema overwrite the prior
93
- * registration (last-write-wins).
108
+ * Two call shapes:
94
109
  *
95
- * @example
96
- * const FileSchema = z.instanceof(File);
97
- * overrideJSONSchema(FileSchema, { type: 'string', format: 'binary' });
110
+ * ```ts
111
+ * // 1. Single fragment — applied verbatim to both input and output emission.
112
+ * overrideJSONSchema(FileSchema, { type: 'string', format: 'binary' });
113
+ *
114
+ * // 2. Divergent fragments — separate fragments per emission side. Omit a
115
+ * // side to leave Zod's default emission untouched on that side.
116
+ * overrideJSONSchema(arrayOrItem, {
117
+ * input: { anyOf: [arrFrag, itemFrag] },
118
+ * output: arrFrag,
119
+ * });
120
+ * ```
98
121
  *
99
- * class UploadDto extends createZodDto(z.object({ file: FileSchema })) {}
100
- * // OpenAPI doc emits `properties.file = { type: 'string', format: 'binary' }`
122
+ * Idempotent: subsequent calls for the same schema overwrite the prior
123
+ * registration (last-write-wins).
101
124
  */
102
- declare const overrideJSONSchema: (schema: z.ZodType, jsonSchema: SchemaObject) => void;
125
+ declare const overrideJSONSchema: (schema: z.ZodType, arg: OverrideJSONSchemaArg) => void;
103
126
 
104
127
  interface ToOpenApiOptions {
105
128
  io: 'input' | 'output';
@@ -549,4 +572,4 @@ declare class ZodNestDocumentError extends ZodNestError {
549
572
  constructor(code: ZodNestDocumentErrorCode, message: string, details?: Record<string, unknown>);
550
573
  }
551
574
 
552
- export { type ApplyZodNestOptions, COMPONENTS_SCHEMAS_PREFIX, type CreateSerializationException, type CreateValidationException, type CreateZodDtoOptions, DEFAULT_MAX_LOGGED_VALUE_BYTES, DEFAULT_REDACT_KEYS, type Io, type LineageEntry, type NormalizedZodNestOptions, type Override, type OverrideContext, type ResponseStatusInput, type ResponseStatusWildcard, 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, extend, getLineage, isZodDto, isZodDtoMarker, makeZodDtoMarker, overrideJSONSchema, resolveEffectiveStatus, toOpenApi };
575
+ export { type ApplyZodNestOptions, COMPONENTS_SCHEMAS_PREFIX, type CreateSerializationException, type CreateValidationException, type CreateZodDtoOptions, DEFAULT_MAX_LOGGED_VALUE_BYTES, DEFAULT_REDACT_KEYS, type Io, type LineageEntry, type NormalizedZodNestOptions, type Override, type OverrideContext, type OverrideJSONSchemaArg, type ResponseStatusInput, type ResponseStatusWildcard, 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, extend, getLineage, isZodDto, isZodDtoMarker, makeZodDtoMarker, overrideJSONSchema, resolveEffectiveStatus, toOpenApi };
package/dist/index.d.ts CHANGED
@@ -79,27 +79,50 @@ declare const extend: <P extends z.ZodObject, S extends z.ZodObject>(parent: P,
79
79
  */
80
80
  declare const getLineage: (schema: z.ZodType) => LineageEntry | undefined;
81
81
 
82
+ /**
83
+ * Argument shape for {@link overrideJSONSchema}. Two forms:
84
+ *
85
+ * - A raw `SchemaObject` — applied to both input and output emission.
86
+ * - A `{ input?, output? }` wrapper — distinct fragments per emission side,
87
+ * for coercion shapes where input and output diverge (e.g.
88
+ * `z.union([z.array(item), item.transform((v) => [v])])` accepts
89
+ * `T | T[]` on input but always emits `T[]` on output).
90
+ *
91
+ * The wrapper form is detected by the presence of an `input` or `output`
92
+ * key. Neither is a JSON Schema / OpenAPI 3.1 keyword, so the discriminator
93
+ * is unambiguous in practice.
94
+ */
95
+ type OverrideJSONSchemaArg = SchemaObject | {
96
+ input?: SchemaObject;
97
+ output?: SchemaObject;
98
+ };
82
99
  /**
83
100
  * Register a fixed JSON Schema fragment for a specific Zod schema instance.
84
101
  *
85
102
  * Designed for shapes JSON Schema can't model directly — `z.custom<T>()` and
86
103
  * `z.instanceof(...)` (e.g. multipart `File` fields) — which Zod emits as `{}`,
87
- * tripping `ZodNestUnrepresentableError` in strict mode. After registration
88
- * the engine writes the supplied fragment in-place wherever that schema
89
- * instance is emitted (single-schema `toOpenApi`, bulk emission, nested
90
- * inside `z.object({...})`, anywhere).
104
+ * tripping `ZodNestUnrepresentableError` in strict mode. Also useful for
105
+ * coercion shapes where input and output diverge (`singleOrArray`-style
106
+ * helpers).
91
107
  *
92
- * Idempotent: subsequent calls for the same schema overwrite the prior
93
- * registration (last-write-wins).
108
+ * Two call shapes:
94
109
  *
95
- * @example
96
- * const FileSchema = z.instanceof(File);
97
- * overrideJSONSchema(FileSchema, { type: 'string', format: 'binary' });
110
+ * ```ts
111
+ * // 1. Single fragment — applied verbatim to both input and output emission.
112
+ * overrideJSONSchema(FileSchema, { type: 'string', format: 'binary' });
113
+ *
114
+ * // 2. Divergent fragments — separate fragments per emission side. Omit a
115
+ * // side to leave Zod's default emission untouched on that side.
116
+ * overrideJSONSchema(arrayOrItem, {
117
+ * input: { anyOf: [arrFrag, itemFrag] },
118
+ * output: arrFrag,
119
+ * });
120
+ * ```
98
121
  *
99
- * class UploadDto extends createZodDto(z.object({ file: FileSchema })) {}
100
- * // OpenAPI doc emits `properties.file = { type: 'string', format: 'binary' }`
122
+ * Idempotent: subsequent calls for the same schema overwrite the prior
123
+ * registration (last-write-wins).
101
124
  */
102
- declare const overrideJSONSchema: (schema: z.ZodType, jsonSchema: SchemaObject) => void;
125
+ declare const overrideJSONSchema: (schema: z.ZodType, arg: OverrideJSONSchemaArg) => void;
103
126
 
104
127
  interface ToOpenApiOptions {
105
128
  io: 'input' | 'output';
@@ -549,4 +572,4 @@ declare class ZodNestDocumentError extends ZodNestError {
549
572
  constructor(code: ZodNestDocumentErrorCode, message: string, details?: Record<string, unknown>);
550
573
  }
551
574
 
552
- export { type ApplyZodNestOptions, COMPONENTS_SCHEMAS_PREFIX, type CreateSerializationException, type CreateValidationException, type CreateZodDtoOptions, DEFAULT_MAX_LOGGED_VALUE_BYTES, DEFAULT_REDACT_KEYS, type Io, type LineageEntry, type NormalizedZodNestOptions, type Override, type OverrideContext, type ResponseStatusInput, type ResponseStatusWildcard, 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, extend, getLineage, isZodDto, isZodDtoMarker, makeZodDtoMarker, overrideJSONSchema, resolveEffectiveStatus, toOpenApi };
575
+ export { type ApplyZodNestOptions, COMPONENTS_SCHEMAS_PREFIX, type CreateSerializationException, type CreateValidationException, type CreateZodDtoOptions, DEFAULT_MAX_LOGGED_VALUE_BYTES, DEFAULT_REDACT_KEYS, type Io, type LineageEntry, type NormalizedZodNestOptions, type Override, type OverrideContext, type OverrideJSONSchemaArg, type ResponseStatusInput, type ResponseStatusWildcard, 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, extend, getLineage, isZodDto, isZodDtoMarker, makeZodDtoMarker, overrideJSONSchema, resolveEffectiveStatus, toOpenApi };
package/dist/index.js CHANGED
@@ -106,19 +106,36 @@ var createCompositionOverride = /* @__PURE__ */ __name((opts) => {
106
106
 
107
107
  // src/schema/custom-override.ts
108
108
  var customOverrideMap = /* @__PURE__ */ new WeakMap();
109
- var overrideJSONSchema = /* @__PURE__ */ __name((schema, jsonSchema) => {
110
- customOverrideMap.set(schema, jsonSchema);
111
- }, "overrideJSONSchema");
112
- var customOverride = /* @__PURE__ */ __name(({ zodSchema, jsonSchema }) => {
113
- const fragment = customOverrideMap.get(zodSchema);
114
- if (fragment === void 0) {
109
+ var isWrapper = /* @__PURE__ */ __name((arg) => "input" in arg || "output" in arg, "isWrapper");
110
+ var overrideJSONSchema = /* @__PURE__ */ __name((schema, arg) => {
111
+ if (isWrapper(arg)) {
112
+ customOverrideMap.set(schema, {
113
+ input: arg.input,
114
+ output: arg.output
115
+ });
115
116
  return;
116
117
  }
117
- for (const key of Object.keys(jsonSchema)) {
118
- Reflect.deleteProperty(jsonSchema, key);
119
- }
120
- Object.assign(jsonSchema, fragment);
121
- }, "customOverride");
118
+ customOverrideMap.set(schema, {
119
+ input: arg,
120
+ output: arg
121
+ });
122
+ }, "overrideJSONSchema");
123
+ var createCustomOverride = /* @__PURE__ */ __name((io) => {
124
+ return ({ zodSchema, jsonSchema }) => {
125
+ const record = customOverrideMap.get(zodSchema);
126
+ if (record === void 0) {
127
+ return;
128
+ }
129
+ const fragment = record[io];
130
+ if (fragment === void 0) {
131
+ return;
132
+ }
133
+ for (const key of Object.keys(jsonSchema)) {
134
+ Reflect.deleteProperty(jsonSchema, key);
135
+ }
136
+ Object.assign(jsonSchema, fragment);
137
+ };
138
+ }, "createCustomOverride");
122
139
 
123
140
  // src/schema/errors.ts
124
141
  var ZodNestError = class extends Error {
@@ -248,6 +265,7 @@ var buildToJsonSchemaOptions = /* @__PURE__ */ __name((params) => {
248
265
  buildRef: params.uri ?? DEFAULT_BUILD_REF,
249
266
  registry: params.registry
250
267
  });
268
+ const customOverride = createCustomOverride(params.io);
251
269
  const merged = combine(primitiveOverride, compositionOverride, customOverride, params.override);
252
270
  const unrepresentableHits = [];
253
271
  const wrapped = /* @__PURE__ */ __name((ctx) => {