diff --git a/deno/lib/__tests__/catch.test.ts b/deno/lib/__tests__/catch.test.ts index 84c5bd4d9..935c39681 100644 --- a/deno/lib/__tests__/catch.test.ts +++ b/deno/lib/__tests__/catch.test.ts @@ -219,3 +219,13 @@ test("catch error", () => { catchError !== undefined && (catchError as z.ZodError).issues[0].message ).toMatch("string"); }); + +test("ctx.input", () => { + const schema = z.string().catch((ctx) => { + console.log(ctx.input); + console.log(ctx.error); + return String(ctx.input); + }); + + expect(schema.parse(123)).toEqual("123"); +}); diff --git a/deno/lib/types.ts b/deno/lib/types.ts index e6977eaba..f61e449a3 100644 --- a/deno/lib/types.ts +++ b/deno/lib/types.ts @@ -464,7 +464,9 @@ export abstract class ZodType< } catch(def: Output): ZodCatch; - catch(def: (ctx: { error: ZodError }) => Output): ZodCatch; + catch( + def: (ctx: { error: ZodError; input: Input }) => Output + ): ZodCatch; catch(def: any) { const catchValueFunc = typeof def === "function" ? def : () => def; @@ -4514,12 +4516,10 @@ export class ZodDefault extends ZodType< ////////// ////////// ////////////////////////////////////////// ////////////////////////////////////////// -export interface ZodCatchDef< - T extends ZodTypeAny = ZodTypeAny, - C extends T["_input"] = T["_input"] -> extends ZodTypeDef { +export interface ZodCatchDef + extends ZodTypeDef { innerType: T; - catchValue: (ctx: { error: ZodError }) => C; + catchValue: (ctx: { error: ZodError; input: unknown }) => T["_input"]; typeName: ZodFirstPartyTypeKind.ZodCatch; } @@ -4559,6 +4559,7 @@ export class ZodCatch extends ZodType< get error() { return new ZodError(newCtx.common.issues); }, + input: newCtx.data, }), }; }); @@ -4572,6 +4573,7 @@ export class ZodCatch extends ZodType< get error() { return new ZodError(newCtx.common.issues); }, + input: newCtx.data, }), }; } diff --git a/package.json b/package.json index ea41800c5..e57bdae93 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "zod", - "version": "3.21.1", + "version": "3.21.2", "author": "Colin McDonnell ", "repository": { "type": "git", diff --git a/src/__tests__/catch.test.ts b/src/__tests__/catch.test.ts index 5a02ce894..4586e4742 100644 --- a/src/__tests__/catch.test.ts +++ b/src/__tests__/catch.test.ts @@ -218,3 +218,13 @@ test("catch error", () => { catchError !== undefined && (catchError as z.ZodError).issues[0].message ).toMatch("string"); }); + +test("ctx.input", () => { + const schema = z.string().catch((ctx) => { + console.log(ctx.input); + console.log(ctx.error); + return String(ctx.input); + }); + + expect(schema.parse(123)).toEqual("123"); +}); diff --git a/src/types.ts b/src/types.ts index 1862be0dd..1336383ca 100644 --- a/src/types.ts +++ b/src/types.ts @@ -464,7 +464,9 @@ export abstract class ZodType< } catch(def: Output): ZodCatch; - catch(def: (ctx: { error: ZodError }) => Output): ZodCatch; + catch( + def: (ctx: { error: ZodError; input: Input }) => Output + ): ZodCatch; catch(def: any) { const catchValueFunc = typeof def === "function" ? def : () => def; @@ -4514,12 +4516,10 @@ export class ZodDefault extends ZodType< ////////// ////////// ////////////////////////////////////////// ////////////////////////////////////////// -export interface ZodCatchDef< - T extends ZodTypeAny = ZodTypeAny, - C extends T["_input"] = T["_input"] -> extends ZodTypeDef { +export interface ZodCatchDef + extends ZodTypeDef { innerType: T; - catchValue: (ctx: { error: ZodError }) => C; + catchValue: (ctx: { error: ZodError; input: unknown }) => T["_input"]; typeName: ZodFirstPartyTypeKind.ZodCatch; } @@ -4559,6 +4559,7 @@ export class ZodCatch extends ZodType< get error() { return new ZodError(newCtx.common.issues); }, + input: newCtx.data, }), }; }); @@ -4572,6 +4573,7 @@ export class ZodCatch extends ZodType< get error() { return new ZodError(newCtx.common.issues); }, + input: newCtx.data, }), }; }