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 97ab9f2
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 54 deletions.
60 changes: 34 additions & 26 deletions deno/lib/__tests__/transformer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,43 @@ 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", () => {
describe("transform ctx.addIssue", () => {
const strs = ["foo", "bar"];

expect(() => {
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;
})
.parse("asdf");
}).toThrow(
JSON.stringify(
[
{
code: "custom",
message: "asdf is not one of our allowed strings",
path: [],
},
],
null,
2
)
const Schema = 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;
});

const expectedError = JSON.stringify(
[
{
code: "custom",
message: "asdf is not one of our allowed strings",
path: [],
},
],
null,
2
);

test("with parse", () => {
expect(() => {
Schema.parse("asdf");
}).toThrow(expectedError);
});

test("with parseAsync", () => {
expect(async () => {
await Schema.parseAsync("asdf");
}).rejects.toThrow(expectedError);
});
});

test("basic transformations", () => {
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
60 changes: 34 additions & 26 deletions src/__tests__/transformer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,43 @@ 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", () => {
describe("transform ctx.addIssue", () => {
const strs = ["foo", "bar"];

expect(() => {
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;
})
.parse("asdf");
}).toThrow(
JSON.stringify(
[
{
code: "custom",
message: "asdf is not one of our allowed strings",
path: [],
},
],
null,
2
)
const Schema = 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;
});

const expectedError = JSON.stringify(
[
{
code: "custom",
message: "asdf is not one of our allowed strings",
path: [],
},
],
null,
2
);

test("with parse", () => {
expect(() => {
Schema.parse("asdf");
}).toThrow(expectedError);
});

test("with parseAsync", () => {
expect(async () => {
await Schema.parseAsync("asdf");
}).rejects.toThrow(expectedError);
});
});

test("basic transformations", () => {
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 97ab9f2

Please sign in to comment.