zod-nest 1.0.0 → 1.1.1

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
 
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,37 @@ 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 peekRegistration = /* @__PURE__ */ __name((schema) => customOverrideMap.get(schema), "peekRegistration");
124
+ var createCustomOverride = /* @__PURE__ */ __name((io) => {
125
+ return ({ zodSchema, jsonSchema }) => {
126
+ const record = customOverrideMap.get(zodSchema);
127
+ if (record === void 0) {
128
+ return;
129
+ }
130
+ const fragment = record[io];
131
+ if (fragment === void 0) {
132
+ return;
133
+ }
134
+ for (const key of Object.keys(jsonSchema)) {
135
+ Reflect.deleteProperty(jsonSchema, key);
136
+ }
137
+ Object.assign(jsonSchema, fragment);
138
+ };
139
+ }, "createCustomOverride");
122
140
 
123
141
  // src/schema/errors.ts
124
142
  var ZodNestError = class extends Error {
@@ -242,16 +260,36 @@ var isStrictlyUnrepresentable = /* @__PURE__ */ __name((jsonSchema, zodSchema) =
242
260
  }
243
261
  return Object.keys(jsonSchema).length === 0;
244
262
  }, "isStrictlyUnrepresentable");
263
+ var markPipeCoverage = /* @__PURE__ */ __name((schema, io, covered) => {
264
+ const def = schema._zod.def;
265
+ if (def.type !== "pipe" || def.in === void 0 || def.out === void 0) {
266
+ return;
267
+ }
268
+ const target = io === "output" ? def.out : def.in._zod.traits.has("$ZodTransform") ? def.out : def.in;
269
+ if (covered.has(target)) {
270
+ return;
271
+ }
272
+ covered.add(target);
273
+ markPipeCoverage(target, io, covered);
274
+ }, "markPipeCoverage");
245
275
  var buildToJsonSchemaOptions = /* @__PURE__ */ __name((params) => {
246
276
  const strict = params.strict ?? true;
247
277
  const compositionOverride = createCompositionOverride({
248
278
  buildRef: params.uri ?? DEFAULT_BUILD_REF,
249
279
  registry: params.registry
250
280
  });
281
+ const customOverride = createCustomOverride(params.io);
251
282
  const merged = combine(primitiveOverride, compositionOverride, customOverride, params.override);
252
283
  const unrepresentableHits = [];
284
+ const coveredByPipe = /* @__PURE__ */ new WeakSet();
253
285
  const wrapped = /* @__PURE__ */ __name((ctx) => {
254
286
  merged(ctx);
287
+ if (ctx.zodSchema._zod.def.type === "pipe") {
288
+ const record = peekRegistration(ctx.zodSchema);
289
+ if (record !== void 0 && record[params.io] !== void 0) {
290
+ markPipeCoverage(ctx.zodSchema, params.io, coveredByPipe);
291
+ }
292
+ }
255
293
  if (!strict || !isStrictlyUnrepresentable(ctx.jsonSchema, ctx.zodSchema)) {
256
294
  return;
257
295
  }
@@ -259,7 +297,8 @@ var buildToJsonSchemaOptions = /* @__PURE__ */ __name((params) => {
259
297
  path: [
260
298
  ...ctx.path
261
299
  ],
262
- zodType: ctx.zodSchema._zod.def.type
300
+ zodType: ctx.zodSchema._zod.def.type,
301
+ zodSchema: ctx.zodSchema
263
302
  });
264
303
  }, "wrapped");
265
304
  const options = {
@@ -277,7 +316,7 @@ var buildToJsonSchemaOptions = /* @__PURE__ */ __name((params) => {
277
316
  return {
278
317
  options,
279
318
  consumeUnrepresentable: /* @__PURE__ */ __name(() => {
280
- const firstHit = unrepresentableHits[0];
319
+ const firstHit = unrepresentableHits.find((hit) => !coveredByPipe.has(hit.zodSchema));
281
320
  if (firstHit !== void 0) {
282
321
  throw new ZodNestUnrepresentableError(firstHit.path, firstHit.zodType);
283
322
  }