Skip to content

Commit

Permalink
Pass input into catchValue
Browse files Browse the repository at this point in the history
  • Loading branch information
colinhacks committed Mar 6, 2023
1 parent f9895ab commit ac0135e
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 13 deletions.
10 changes: 10 additions & 0 deletions deno/lib/__tests__/catch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
14 changes: 8 additions & 6 deletions deno/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,9 @@ export abstract class ZodType<
}

catch(def: Output): ZodCatch<this>;
catch(def: (ctx: { error: ZodError }) => Output): ZodCatch<this>;
catch(
def: (ctx: { error: ZodError; input: Input }) => Output
): ZodCatch<this>;
catch(def: any) {
const catchValueFunc = typeof def === "function" ? def : () => def;

Expand Down Expand Up @@ -4514,12 +4516,10 @@ export class ZodDefault<T extends ZodTypeAny> extends ZodType<
////////// //////////
//////////////////////////////////////////
//////////////////////////////////////////
export interface ZodCatchDef<
T extends ZodTypeAny = ZodTypeAny,
C extends T["_input"] = T["_input"]
> extends ZodTypeDef {
export interface ZodCatchDef<T extends ZodTypeAny = ZodTypeAny>
extends ZodTypeDef {
innerType: T;
catchValue: (ctx: { error: ZodError }) => C;
catchValue: (ctx: { error: ZodError; input: unknown }) => T["_input"];
typeName: ZodFirstPartyTypeKind.ZodCatch;
}

Expand Down Expand Up @@ -4559,6 +4559,7 @@ export class ZodCatch<T extends ZodTypeAny> extends ZodType<
get error() {
return new ZodError(newCtx.common.issues);
},
input: newCtx.data,
}),
};
});
Expand All @@ -4572,6 +4573,7 @@ export class ZodCatch<T extends ZodTypeAny> extends ZodType<
get error() {
return new ZodError(newCtx.common.issues);
},
input: newCtx.data,
}),
};
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zod",
"version": "3.21.1",
"version": "3.21.2",
"author": "Colin McDonnell <colin@colinhacks.com>",
"repository": {
"type": "git",
Expand Down
10 changes: 10 additions & 0 deletions src/__tests__/catch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
14 changes: 8 additions & 6 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,9 @@ export abstract class ZodType<
}

catch(def: Output): ZodCatch<this>;
catch(def: (ctx: { error: ZodError }) => Output): ZodCatch<this>;
catch(
def: (ctx: { error: ZodError; input: Input }) => Output
): ZodCatch<this>;
catch(def: any) {
const catchValueFunc = typeof def === "function" ? def : () => def;

Expand Down Expand Up @@ -4514,12 +4516,10 @@ export class ZodDefault<T extends ZodTypeAny> extends ZodType<
////////// //////////
//////////////////////////////////////////
//////////////////////////////////////////
export interface ZodCatchDef<
T extends ZodTypeAny = ZodTypeAny,
C extends T["_input"] = T["_input"]
> extends ZodTypeDef {
export interface ZodCatchDef<T extends ZodTypeAny = ZodTypeAny>
extends ZodTypeDef {
innerType: T;
catchValue: (ctx: { error: ZodError }) => C;
catchValue: (ctx: { error: ZodError; input: unknown }) => T["_input"];
typeName: ZodFirstPartyTypeKind.ZodCatch;
}

Expand Down Expand Up @@ -4559,6 +4559,7 @@ export class ZodCatch<T extends ZodTypeAny> extends ZodType<
get error() {
return new ZodError(newCtx.common.issues);
},
input: newCtx.data,
}),
};
});
Expand All @@ -4572,6 +4573,7 @@ export class ZodCatch<T extends ZodTypeAny> extends ZodType<
get error() {
return new ZodError(newCtx.common.issues);
},
input: newCtx.data,
}),
};
}
Expand Down

0 comments on commit ac0135e

Please sign in to comment.