Skip to content

Commit

Permalink
Only call .catch() method when parsing fails
Browse files Browse the repository at this point in the history
  • Loading branch information
tmcw committed Dec 12, 2022
1 parent 1298d26 commit 95d6cd1
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
6 changes: 6 additions & 0 deletions deno/lib/__tests__/catch.test.ts
Expand Up @@ -9,6 +9,12 @@ test("basic catch", () => {
expect(z.string().catch("default").parse(undefined)).toBe("default");
});

test("catch fn does not run when parsing succeeds", () => {
const cb = jest.fn().mockReturnValue("x") as () => string;
expect(z.string().catch(cb).parse("test")).toBe("test");
expect(cb).not.toBeCalled();
});

test("basic catch async", async () => {
const result = await z.string().catch("default").parseAsync(1243);
expect(result).toBe("default");
Expand Down
8 changes: 4 additions & 4 deletions deno/lib/types.ts
Expand Up @@ -4076,17 +4076,17 @@ export class ZodCatch<T extends ZodTypeAny> extends ZodType<

if (isAsync(result)) {
return result.then((result) => {
const defaultValue = this._def.defaultValue();
return {
status: "valid",
value: result.status === "valid" ? result.value : defaultValue,
value:
result.status === "valid" ? result.value : this._def.defaultValue(),
};
});
} else {
const defaultValue = this._def.defaultValue();
return {
status: "valid",
value: result.status === "valid" ? result.value : defaultValue,
value:
result.status === "valid" ? result.value : this._def.defaultValue(),
};
}
}
Expand Down
8 changes: 7 additions & 1 deletion src/__tests__/catch.test.ts
@@ -1,5 +1,5 @@
// @ts-ignore TS6133
import { expect, test } from "@jest/globals";
import { expect, jest, test } from "@jest/globals";

import { z } from "..";
import { util } from "../helpers/util";
Expand All @@ -8,6 +8,12 @@ test("basic catch", () => {
expect(z.string().catch("default").parse(undefined)).toBe("default");
});

test("catch fn does not run when parsing succeeds", () => {
const cb = jest.fn().mockReturnValue("x") as () => string;
expect(z.string().catch(cb).parse("test")).toBe("test");
expect(cb).not.toBeCalled();
});

test("basic catch async", async () => {
const result = await z.string().catch("default").parseAsync(1243);
expect(result).toBe("default");
Expand Down
8 changes: 4 additions & 4 deletions src/types.ts
Expand Up @@ -4076,17 +4076,17 @@ export class ZodCatch<T extends ZodTypeAny> extends ZodType<

if (isAsync(result)) {
return result.then((result) => {
const defaultValue = this._def.defaultValue();
return {
status: "valid",
value: result.status === "valid" ? result.value : defaultValue,
value:
result.status === "valid" ? result.value : this._def.defaultValue(),
};
});
} else {
const defaultValue = this._def.defaultValue();
return {
status: "valid",
value: result.status === "valid" ? result.value : defaultValue,
value:
result.status === "valid" ? result.value : this._def.defaultValue(),
};
}
}
Expand Down

0 comments on commit 95d6cd1

Please sign in to comment.