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

Class ZodTuple

packages/zod/src/v3/types.ts:3395–3477  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3393export type AnyZodTuple = ZodTuple<[ZodTypeAny, ...ZodTypeAny[]] | [], ZodTypeAny | null>;
3394// type ZodTupleItems = [ZodTypeAny, ...ZodTypeAny[]];
3395export class ZodTuple<
3396 T extends ZodTupleItems | [] = ZodTupleItems,
3397 Rest extends ZodTypeAny | null = null,
3398> extends ZodType<OutputTypeOfTupleWithRest<T, Rest>, ZodTupleDef<T, Rest>, InputTypeOfTupleWithRest<T, Rest>> {
3399 _parse(input: ParseInput): ParseReturnType<this["_output"]> {
3400 const { status, ctx } = this._processInputParams(input);
3401 if (ctx.parsedType !== ZodParsedType.array) {
3402 addIssueToContext(ctx, {
3403 code: ZodIssueCode.invalid_type,
3404 expected: ZodParsedType.array,
3405 received: ctx.parsedType,
3406 });
3407 return INVALID;
3408 }
3409
3410 if (ctx.data.length < this._def.items.length) {
3411 addIssueToContext(ctx, {
3412 code: ZodIssueCode.too_small,
3413 minimum: this._def.items.length,
3414 inclusive: true,
3415 exact: false,
3416 type: "array",
3417 });
3418
3419 return INVALID;
3420 }
3421
3422 const rest = this._def.rest;
3423
3424 if (!rest && ctx.data.length > this._def.items.length) {
3425 addIssueToContext(ctx, {
3426 code: ZodIssueCode.too_big,
3427 maximum: this._def.items.length,
3428 inclusive: true,
3429 exact: false,
3430 type: "array",
3431 });
3432 status.dirty();
3433 }
3434
3435 const items = ([...ctx.data] as any[])
3436 .map((item, itemIndex) => {
3437 const schema = this._def.items[itemIndex] || this._def.rest;
3438 if (!schema) return null as any as SyncParseReturnType<any>;
3439 return schema._parse(new ParseInputLazyPath(ctx, item, ctx.path, itemIndex));
3440 })
3441 .filter((x) => !!x); // filter nulls
3442
3443 if (ctx.common.async) {
3444 return Promise.all(items).then((results) => {
3445 return ParseStatus.mergeArray(status, results);
3446 });
3447 } else {
3448 return ParseStatus.mergeArray(status, items as SyncParseReturnType[]);
3449 }
3450 }
3451
3452 get items() {

Callers

nothing calls this directly

Calls 1

processCreateParamsFunction · 0.85

Tested by

no test coverage detected