MCPcopy
hub / github.com/colinhacks/zod / tupleProcessor

Function tupleProcessor

packages/zod/src/v4/core/json-schema-processors.ts:373–426  ·  view source on GitHub ↗
(schema, ctx, _json, params)

Source from the content-addressed store, hash-verified

371};
372
373export const tupleProcessor: Processor<schemas.$ZodTuple> = (schema, ctx, _json, params) => {
374 const json = _json as JSONSchema.ArraySchema;
375 const def = schema._zod.def as schemas.$ZodTupleDef;
376 json.type = "array";
377
378 const prefixPath = ctx.target === "draft-2020-12" ? "prefixItems" : "items";
379 const restPath =
380 ctx.target === "draft-2020-12" ? "items" : ctx.target === "openapi-3.0" ? "items" : "additionalItems";
381
382 const prefixItems = def.items.map((x, i) =>
383 process(x, ctx as any, {
384 ...params,
385 path: [...params.path, prefixPath, i],
386 })
387 );
388 const rest = def.rest
389 ? process(def.rest, ctx as any, {
390 ...params,
391 path: [...params.path, restPath, ...(ctx.target === "openapi-3.0" ? [def.items.length] : [])],
392 })
393 : null;
394
395 if (ctx.target === "draft-2020-12") {
396 json.prefixItems = prefixItems;
397 if (rest) {
398 json.items = rest;
399 }
400 } else if (ctx.target === "openapi-3.0") {
401 json.items = {
402 anyOf: prefixItems,
403 };
404
405 if (rest) {
406 json.items.anyOf!.push(rest);
407 }
408 json.minItems = prefixItems.length;
409 if (!rest) {
410 json.maxItems = prefixItems.length;
411 }
412 } else {
413 json.items = prefixItems;
414 if (rest) {
415 json.additionalItems = rest;
416 }
417 }
418
419 // length
420 const { minimum, maximum } = schema._zod.bag as {
421 minimum?: number;
422 maximum?: number;
423 };
424 if (typeof minimum === "number") json.minItems = minimum;
425 if (typeof maximum === "number") json.maxItems = maximum;
426};
427
428export const recordProcessor: Processor<schemas.$ZodRecord> = (schema, ctx, _json, params) => {
429 const json = _json as JSONSchema.ObjectSchema;

Callers

nothing calls this directly

Calls 1

processFunction · 0.90

Tested by

no test coverage detected