Skip to content

Commit

Permalink
Fix transform ctx.addIssue to work with parseAsync
Browse files Browse the repository at this point in the history
  • Loading branch information
bentefay committed May 11, 2022
1 parent cb2b86c commit b36cf43
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 4 deletions.
34 changes: 33 additions & 1 deletion deno/lib/__tests__/transformer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const stringToNumber = z.string().transform((arg) => parseFloat(arg));
// .transform((n) => String(n));
const asyncNumberToString = z.number().transform(async (n) => String(n));

test("transform ctx.addIssue", () => {
test("transform ctx.addIssue with parse", () => {
const strs = ["foo", "bar"];

expect(() => {
Expand Down Expand Up @@ -42,6 +42,38 @@ test("transform ctx.addIssue", () => {
);
});

test("transform ctx.addIssue with parseAsync", async () => {
const strs = ["foo", "bar"];

const result = await z
.string()
.transform((data, ctx) => {
const i = strs.indexOf(data);
if (i === -1) {
ctx.addIssue({
code: "custom",
message: `${data} is not one of our allowed strings`,
});
}
return data.length;
})
.safeParseAsync("asdf");

expect(JSON.parse(JSON.stringify(result))).toEqual({
success: false,
error: {
issues: [
{
code: "custom",
message: "asdf is not one of our allowed strings",
path: [],
},
],
name: "ZodError",
},
});
});

test("basic transformations", () => {
const r1 = z
.string()
Expand Down
2 changes: 1 addition & 1 deletion deno/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3328,7 +3328,7 @@ export class ZodEffects<
// return { status: "dirty", value: base.value };
// }
return Promise.resolve(effect.transform(base.value, checkCtx)).then(
OK
(result) => ({ status: status.value, value: result })
);
});
}
Expand Down
34 changes: 33 additions & 1 deletion src/__tests__/transformer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const stringToNumber = z.string().transform((arg) => parseFloat(arg));
// .transform((n) => String(n));
const asyncNumberToString = z.number().transform(async (n) => String(n));

test("transform ctx.addIssue", () => {
test("transform ctx.addIssue with parse", () => {
const strs = ["foo", "bar"];

expect(() => {
Expand Down Expand Up @@ -41,6 +41,38 @@ test("transform ctx.addIssue", () => {
);
});

test("transform ctx.addIssue with parseAsync", async () => {
const strs = ["foo", "bar"];

const result = await z
.string()
.transform((data, ctx) => {
const i = strs.indexOf(data);
if (i === -1) {
ctx.addIssue({
code: "custom",
message: `${data} is not one of our allowed strings`,
});
}
return data.length;
})
.safeParseAsync("asdf");

expect(JSON.parse(JSON.stringify(result))).toEqual({
success: false,
error: {
issues: [
{
code: "custom",
message: "asdf is not one of our allowed strings",
path: [],
},
],
name: "ZodError",
},
});
});

test("basic transformations", () => {
const r1 = z
.string()
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3328,7 +3328,7 @@ export class ZodEffects<
// return { status: "dirty", value: base.value };
// }
return Promise.resolve(effect.transform(base.value, checkCtx)).then(
OK
(result) => ({ status: status.value, value: result })
);
});
}
Expand Down

0 comments on commit b36cf43

Please sign in to comment.