diff --git a/deno/lib/__tests__/transformer.test.ts b/deno/lib/__tests__/transformer.test.ts index fb1e48c34e..d83c5f6eb3 100644 --- a/deno/lib/__tests__/transformer.test.ts +++ b/deno/lib/__tests__/transformer.test.ts @@ -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", () => { diff --git a/deno/lib/types.ts b/deno/lib/types.ts index 0c3c160b85..1c807d360e 100644 --- a/deno/lib/types.ts +++ b/deno/lib/types.ts @@ -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 }) ); }); } diff --git a/src/__tests__/transformer.test.ts b/src/__tests__/transformer.test.ts index 725802365d..cf7bfb1815 100644 --- a/src/__tests__/transformer.test.ts +++ b/src/__tests__/transformer.test.ts @@ -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", () => { diff --git a/src/types.ts b/src/types.ts index 8d4018e755..7fc7417883 100644 --- a/src/types.ts +++ b/src/types.ts @@ -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 }) ); }); }