Skip to content

Commit

Permalink
Only call .catch() method when parsing fails (#1674)
Browse files Browse the repository at this point in the history
* Only call .catch() method when parsing fails

* Fix deno test

Co-authored-by: Colin McDonnell <colinmcd@alum.mit.edu>
  • Loading branch information
tmcw and Colin McDonnell committed Dec 12, 2022
1 parent 1298d26 commit b3b0ecf
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
10 changes: 10 additions & 0 deletions deno/lib/__tests__/catch.test.ts
Expand Up @@ -9,6 +9,16 @@ test("basic catch", () => {
expect(z.string().catch("default").parse(undefined)).toBe("default");
});

test("catch fn does not run when parsing succeeds", () => {
let isCalled = false;
const cb = () => {
isCalled = true;
return "asdf";
};
expect(z.string().catch(cb).parse("test")).toBe("test");
expect(isCalled).toEqual(false);
});

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
10 changes: 10 additions & 0 deletions src/__tests__/catch.test.ts
Expand Up @@ -8,6 +8,16 @@ test("basic catch", () => {
expect(z.string().catch("default").parse(undefined)).toBe("default");
});

test("catch fn does not run when parsing succeeds", () => {
let isCalled = false;
const cb = () => {
isCalled = true;
return "asdf";
};
expect(z.string().catch(cb).parse("test")).toBe("test");
expect(isCalled).toEqual(false);
});

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 b3b0ecf

Please sign in to comment.