From 3ce9d214638cf7b700e5258557f55451a723a3a6 Mon Sep 17 00:00:00 2001 From: mathcovax Date: Fri, 13 Sep 2024 16:49:53 +0000 Subject: [PATCH] feat(47): rework error --- scripts/accelerator.ts | 9 +- scripts/accelerators/array.test.ts | 40 ++++- scripts/accelerators/bigInt.test.ts | 41 ++++- scripts/accelerators/boolean.test.ts | 8 +- scripts/accelerators/date.test.ts | 32 +++- scripts/accelerators/default.test.ts | 8 +- scripts/accelerators/effects.test.ts | 32 +++- scripts/accelerators/effects.ts | 39 ++++- scripts/accelerators/enum.test.ts | 8 +- scripts/accelerators/intersection.test.ts | 40 ++++- scripts/accelerators/intersection.ts | 4 +- scripts/accelerators/lazy.test.ts | 16 +- scripts/accelerators/lazy.ts | 1 - scripts/accelerators/literal.test.ts | 16 +- scripts/accelerators/nan.test.ts | 8 +- scripts/accelerators/never.test.ts | 8 +- scripts/accelerators/null.test.ts | 8 +- scripts/accelerators/nullable.test.ts | 8 +- scripts/accelerators/number.test.ts | 56 ++++++- scripts/accelerators/object.test.ts | 40 ++++- scripts/accelerators/optional.test.ts | 8 +- scripts/accelerators/record.test.ts | 16 +- scripts/accelerators/string.test.ts | 192 +++++++++++++++++++--- scripts/accelerators/symbol.test.ts | 8 +- scripts/accelerators/tuple.test.ts | 16 +- scripts/accelerators/type.test.ts | 8 +- scripts/accelerators/undefined.test.ts | 8 +- scripts/accelerators/union.test.ts | 8 +- scripts/accelerators/void.test.ts | 8 +- scripts/error.ts | 20 ++- 30 files changed, 615 insertions(+), 99 deletions(-) diff --git a/scripts/accelerator.ts b/scripts/accelerator.ts index 6b02645..d131b53 100644 --- a/scripts/accelerator.ts +++ b/scripts/accelerator.ts @@ -39,8 +39,13 @@ export abstract class ZodAccelerator { let $output = ${mayBeAwait} $this.zodSchema.accelerator.${parseMethod}($input); if($output.success === false){ - $output.error.message = $output.error.message.replace(".", \`$path.\`); - return $output; + return { + success: false, + error: new ZodAcceleratorError( + $output.error.passedPath.replace(".", \`$path.\`), + $output.error.passedMessage + ) + } } $input = $output.data; diff --git a/scripts/accelerators/array.test.ts b/scripts/accelerators/array.test.ts index 2c10c5b..cdf89de 100644 --- a/scripts/accelerators/array.test.ts +++ b/scripts/accelerators/array.test.ts @@ -19,7 +19,13 @@ describe("array type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(".[Array 1] : Input is not a String."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ".[Array 1] : Input is not a String.", + path: ["[Array 1]"], + }, + ]); } }); @@ -39,7 +45,13 @@ describe("array type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(".[Array 1].test : Input is not a String."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ".[Array 1].test : Input is not a String.", + path: ["[Array 1]", "test"], + }, + ]); } }); @@ -59,7 +71,13 @@ describe("array type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(". : Input Array has length not equal to 2."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ". : Input Array has length not equal to 2.", + path: [], + }, + ]); } }); @@ -79,7 +97,13 @@ describe("array type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(". : Input Array has length less than 3."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ". : Input Array has length less than 3.", + path: [], + }, + ]); } }); @@ -99,7 +123,13 @@ describe("array type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(". : Input Array has length more than 3."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ". : Input Array has length more than 3.", + path: [], + }, + ]); } }); }); diff --git a/scripts/accelerators/bigInt.test.ts b/scripts/accelerators/bigInt.test.ts index e17ad1d..85ee245 100644 --- a/scripts/accelerators/bigInt.test.ts +++ b/scripts/accelerators/bigInt.test.ts @@ -19,7 +19,13 @@ describe("bigInt type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(". : Input is not BigInt."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ". : Input is not BigInt.", + path: [], + }, + ]); } }); @@ -39,7 +45,13 @@ describe("bigInt type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(". : Input BigInt is less or equal than 1."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ". : Input BigInt is less or equal than 1.", + path: [], + }, + ]); } }); @@ -59,7 +71,13 @@ describe("bigInt type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(". : Input BigInt is more or equal than 4."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ". : Input BigInt is more or equal than 4.", + path: [], + }, + ]); } }); @@ -79,7 +97,13 @@ describe("bigInt type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(". : Input BigInt is not multiple of 4."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ". : Input BigInt is not multiple of 4.", + path: [], + }, + ]); } }); @@ -98,8 +122,13 @@ describe("bigInt type", () => { } catch (error: any) { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); - // expect(schema.safeParse(data).success).toBe(false) - expect(err.message).toBe(". : Input is not BigInt."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ". : Input is not BigInt.", + path: [], + }, + ]); } }); }); diff --git a/scripts/accelerators/boolean.test.ts b/scripts/accelerators/boolean.test.ts index c53c8d8..877b2f7 100644 --- a/scripts/accelerators/boolean.test.ts +++ b/scripts/accelerators/boolean.test.ts @@ -18,7 +18,13 @@ describe("bigInt type", () => { } catch (error: any) { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); - expect(err.message).toBe(". : Input is not boolean."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ". : Input is not boolean.", + path: [], + }, + ]); } }); diff --git a/scripts/accelerators/date.test.ts b/scripts/accelerators/date.test.ts index 90f4b8e..0cb73c6 100644 --- a/scripts/accelerators/date.test.ts +++ b/scripts/accelerators/date.test.ts @@ -19,7 +19,13 @@ describe("date type", () => { const err: ZodAcceleratorError = error; expect(schema.safeParse(data).success).toBe(false); expect(err).instanceOf(ZodAcceleratorError); - expect(err.message).toBe(". : Input is invalide Date."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ". : Input is invalide Date.", + path: [], + }, + ]); } }); @@ -44,7 +50,13 @@ describe("date type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(`. : Input Date is less than ${minDate.getTime()}.`); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: `. : Input Date is less than ${minDate.getTime()}.`, + path: [], + }, + ]); } }); @@ -69,7 +81,13 @@ describe("date type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(`. : Input Date is more than ${minDate.getTime()}.`); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: `. : Input Date is more than ${minDate.getTime()}.`, + path: [], + }, + ]); } }); @@ -89,7 +107,13 @@ describe("date type", () => { const err: ZodAcceleratorError = error; expect(schema.safeParse(data).success).toBe(false); expect(err).instanceOf(ZodAcceleratorError); - expect(err.message).toBe(". : Input is invalide Date."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ". : Input is invalide Date.", + path: [], + }, + ]); } }); }); diff --git a/scripts/accelerators/default.test.ts b/scripts/accelerators/default.test.ts index e09f95e..cf1f402 100644 --- a/scripts/accelerators/default.test.ts +++ b/scripts/accelerators/default.test.ts @@ -19,7 +19,13 @@ describe("default type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(". : Input is not a String."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ". : Input is not a String.", + path: [], + }, + ]); } }); diff --git a/scripts/accelerators/effects.test.ts b/scripts/accelerators/effects.test.ts index a5ec808..371661a 100644 --- a/scripts/accelerators/effects.test.ts +++ b/scripts/accelerators/effects.test.ts @@ -29,7 +29,13 @@ describe("effects type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(". : super message"); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: "super message", + path: [], + }, + ]); } }); @@ -49,7 +55,13 @@ describe("effects type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(". : super message"); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: "super message", + path: [], + }, + ]); } }); @@ -77,7 +89,13 @@ describe("effects type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(". : super message"); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: "super message", + path: [], + }, + ]); } }); @@ -107,7 +125,13 @@ describe("effects type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(". : super message"); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: "super message", + path: [], + }, + ]); } }); }); diff --git a/scripts/accelerators/effects.ts b/scripts/accelerators/effects.ts index 009e7c0..3110c14 100644 --- a/scripts/accelerators/effects.ts +++ b/scripts/accelerators/effects.ts @@ -45,13 +45,20 @@ export class ZodEffectAccelerator extends ZodAccelerator { { path: [], addIssue: (issue) => { - $id_issue = {success: false, error: new ZodAcceleratorError(\`$path\`, issue.message || "Effect transform issue without message.")}; + $id_issue = { + success: false, + error: new ZodAcceleratorError(\`$path\`, "Effect transform issue without message.", issue) + }; } } ) - if($id_issue || $input === this.duploj$Never){ - return $id_issue || {success: false, error: new ZodAcceleratorError(\`$path\`, "Effect return never.")}; + if($id_issue) { + return $id_issue; + } + + if($input === this.duploj$Never){ + return {success: false, error: new ZodAcceleratorError(\`$path\`, "Effect return never.")}; } `, refinement: (isAsync: boolean) => ` @@ -61,14 +68,21 @@ export class ZodEffectAccelerator extends ZodAccelerator { { path: [], addIssue: (issue) => { - $id_issue = {success: false, error: new ZodAcceleratorError(\`$path\`, issue.message || "Effect refinement issue without message.")}; + $id_issue = { + success: false, + error: new ZodAcceleratorError(\`$path\`, "Effect refinement issue without message.", issue) + }; } } ) - if($id_issue || $input === this.duploj$Never){ - return $id_issue || {success: false, error: new ZodAcceleratorError(\`$path\`, "Effect return never.")}; + if($id_issue) { + return $id_issue; + } + + if($input === this.duploj$Never){ + return {success: false, error: new ZodAcceleratorError(\`$path\`, "Effect return never.")}; } `, preprocess: (isAsync: boolean) => ` @@ -78,13 +92,20 @@ export class ZodEffectAccelerator extends ZodAccelerator { { path: [], addIssue: (issue) => { - $id_issue = {success: false, error: new ZodAcceleratorError(\`$path\`, issue.message || "Effect preprocess issue without message.")}; + $id_issue = { + success: false, + error: new ZodAcceleratorError(\`$path\`, "Effect preprocess issue without message.", issue) + }; } } ) - if($id_issue || $input === this.duploj$Never){ - return $id_issue || {success: false, error: new ZodAcceleratorError(\`$path\`, "Effect return never.")}; + if($id_issue) { + return $id_issue; + } + + if($input === this.duploj$Never){ + return {success: false, error: new ZodAcceleratorError(\`$path\`, "Effect return never.")}; } `, }; diff --git a/scripts/accelerators/enum.test.ts b/scripts/accelerators/enum.test.ts index 79f023c..80313a3 100644 --- a/scripts/accelerators/enum.test.ts +++ b/scripts/accelerators/enum.test.ts @@ -19,7 +19,13 @@ describe("enum type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(". : Input is not equal to test or test1 or test2."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ". : Input is not equal to test or test1 or test2.", + path: [], + }, + ]); } }); }); diff --git a/scripts/accelerators/intersection.test.ts b/scripts/accelerators/intersection.test.ts index 3724e15..95404f6 100644 --- a/scripts/accelerators/intersection.test.ts +++ b/scripts/accelerators/intersection.test.ts @@ -22,7 +22,13 @@ describe("intersection type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(". : Input String has length less than 10."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ". : Input String has length less than 10.", + path: [], + }, + ]); } }); @@ -64,7 +70,13 @@ describe("intersection type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(".test3 : Input is not Object."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ".test3 : Input is not Object.", + path: ["test3"], + }, + ]); } }); @@ -106,7 +118,13 @@ describe("intersection type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(". : Intersection results could not be merged."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ". : Intersection results could not be merged.", + path: [], + }, + ]); } data = { @@ -120,7 +138,13 @@ describe("intersection type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(". : Intersection results could not be merged."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ". : Intersection results could not be merged.", + path: [], + }, + ]); } data = { @@ -134,7 +158,13 @@ describe("intersection type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(". : Intersection results could not be merged."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ". : Intersection results could not be merged.", + path: [], + }, + ]); } data = {}; diff --git a/scripts/accelerators/intersection.ts b/scripts/accelerators/intersection.ts index a2f413e..6c8a5f0 100644 --- a/scripts/accelerators/intersection.ts +++ b/scripts/accelerators/intersection.ts @@ -47,8 +47,8 @@ export class ZodIntersectionAccelerator extends ZodAccelerator { } public static contentPart = { - mergeValues: () => /* js */` - let $id_resultMergeValues = this.duploj$mergeValues($id_left_output, $id_right_output); + mergeValues: () => ` + let $id_resultMergeValues = this.duploj$mergeValues($id_left_output, $id_right_output, \`$path\`); if(!$id_resultMergeValues.success){ return $id_resultMergeValues; } diff --git a/scripts/accelerators/lazy.test.ts b/scripts/accelerators/lazy.test.ts index bbb3f2d..19836eb 100644 --- a/scripts/accelerators/lazy.test.ts +++ b/scripts/accelerators/lazy.test.ts @@ -57,7 +57,13 @@ describe("lazy type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(".subcategories.[Array 0].subcategories.[Array 1].name : Input is not a String."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ".subcategories.[Array 0].subcategories.[Array 1].name : Input is not a String.", + path: ["subcategories", "[Array 0]", "subcategories", "[Array 1]", "name"], + }, + ]); } }); @@ -78,7 +84,13 @@ describe("lazy type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(". : Input is not a String."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ". : Input is not a String.", + path: [], + }, + ]); } }); }); diff --git a/scripts/accelerators/lazy.ts b/scripts/accelerators/lazy.ts index 43fa835..1519fdf 100644 --- a/scripts/accelerators/lazy.ts +++ b/scripts/accelerators/lazy.ts @@ -1,6 +1,5 @@ import type * as zod from "zod"; import { ZodAccelerator } from "../accelerator"; -import { ZodAcceleratorContent } from "../content"; @ZodAccelerator.autoInstance export class ZodLazyAccelerator extends ZodAccelerator { diff --git a/scripts/accelerators/literal.test.ts b/scripts/accelerators/literal.test.ts index 0481863..d9640f5 100644 --- a/scripts/accelerators/literal.test.ts +++ b/scripts/accelerators/literal.test.ts @@ -19,7 +19,13 @@ describe("literal type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(". : Input literal is wrong."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ". : Input literal is wrong.", + path: [], + }, + ]); } }); @@ -39,7 +45,13 @@ describe("literal type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(". : Input literal is wrong."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ". : Input literal is wrong.", + path: [], + }, + ]); } }); }); diff --git a/scripts/accelerators/nan.test.ts b/scripts/accelerators/nan.test.ts index db93a23..3dc953b 100644 --- a/scripts/accelerators/nan.test.ts +++ b/scripts/accelerators/nan.test.ts @@ -19,7 +19,13 @@ describe("nan type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(". : Input is not NaN."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ". : Input is not NaN.", + path: [], + }, + ]); } }); }); diff --git a/scripts/accelerators/never.test.ts b/scripts/accelerators/never.test.ts index 38a29a4..752846b 100644 --- a/scripts/accelerators/never.test.ts +++ b/scripts/accelerators/never.test.ts @@ -15,7 +15,13 @@ describe("never type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(". : Input is not never."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ". : Input is not never.", + path: [], + }, + ]); } }); }); diff --git a/scripts/accelerators/null.test.ts b/scripts/accelerators/null.test.ts index 5064020..b3489ac 100644 --- a/scripts/accelerators/null.test.ts +++ b/scripts/accelerators/null.test.ts @@ -19,7 +19,13 @@ describe("null type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(". : Input is not null."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ". : Input is not null.", + path: [], + }, + ]); } }); }); diff --git a/scripts/accelerators/nullable.test.ts b/scripts/accelerators/nullable.test.ts index 34e3b02..0c69819 100644 --- a/scripts/accelerators/nullable.test.ts +++ b/scripts/accelerators/nullable.test.ts @@ -23,7 +23,13 @@ describe("nullable type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(". : Input is not a String."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ". : Input is not a String.", + path: [], + }, + ]); } }); }); diff --git a/scripts/accelerators/number.test.ts b/scripts/accelerators/number.test.ts index 64e873f..1cdab77 100644 --- a/scripts/accelerators/number.test.ts +++ b/scripts/accelerators/number.test.ts @@ -19,7 +19,13 @@ describe("number type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(". : Input is not a Number."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ". : Input is not a Number.", + path: [], + }, + ]); } }); @@ -39,7 +45,13 @@ describe("number type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(". : Input Number is less than 1."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ". : Input Number is less than 1.", + path: [], + }, + ]); } }); @@ -59,7 +71,13 @@ describe("number type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(". : Input Number is more than 1."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ". : Input Number is more than 1.", + path: [], + }, + ]); } }); @@ -79,7 +97,13 @@ describe("number type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(". : Input Number is not multiple of 4."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ". : Input Number is not multiple of 4.", + path: [], + }, + ]); } }); @@ -99,7 +123,13 @@ describe("number type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(". : Input is not Int."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ". : Input is not Int.", + path: [], + }, + ]); } }); @@ -119,7 +149,13 @@ describe("number type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(". : Input Number is not finite."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ". : Input Number is not finite.", + path: [], + }, + ]); } }); @@ -139,7 +175,13 @@ describe("number type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(". : Input is not a Number."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ". : Input is not a Number.", + path: [], + }, + ]); } }); }); diff --git a/scripts/accelerators/object.test.ts b/scripts/accelerators/object.test.ts index 671f2cb..9383e68 100644 --- a/scripts/accelerators/object.test.ts +++ b/scripts/accelerators/object.test.ts @@ -32,7 +32,13 @@ describe("object type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(".test1 : Input is not a String."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ".test1 : Input is not a String.", + path: ["test1"], + }, + ]); } data = 11; @@ -44,7 +50,13 @@ describe("object type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(". : Input is not Object."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ". : Input is not Object.", + path: [], + }, + ]); } }); @@ -73,7 +85,13 @@ describe("object type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(".test2 : Input Object has key to many."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ".test2 : Input Object has key to many.", + path: ["test2"], + }, + ]); } }); @@ -129,7 +147,13 @@ describe("object type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(".ddd.test2 : Input is not a Number."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ".ddd.test2 : Input is not a Number.", + path: ["ddd", "test2"], + }, + ]); } data = { @@ -147,7 +171,13 @@ describe("object type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(".ddd.test3 : Input Object has key to many."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ".ddd.test3 : Input Object has key to many.", + path: ["ddd", "test3"], + }, + ]); } }); }); diff --git a/scripts/accelerators/optional.test.ts b/scripts/accelerators/optional.test.ts index dafae5a..9287490 100644 --- a/scripts/accelerators/optional.test.ts +++ b/scripts/accelerators/optional.test.ts @@ -23,7 +23,13 @@ describe("optinal type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(". : Input is not a String."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ". : Input is not a String.", + path: [], + }, + ]); } }); }); diff --git a/scripts/accelerators/record.test.ts b/scripts/accelerators/record.test.ts index 2d0d845..d89ef55 100644 --- a/scripts/accelerators/record.test.ts +++ b/scripts/accelerators/record.test.ts @@ -24,7 +24,13 @@ describe("record type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(".[Record Value \"test1\"] : Input is not a String."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ".[Record Value \"test1\"] : Input is not a String.", + path: ["[Record Value \"test1\"]"], + }, + ]); } }); @@ -49,7 +55,13 @@ describe("record type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(".[Record Key \"test1\"] : Input is not a Number."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ".[Record Key \"test1\"] : Input is not a Number.", + path: ["[Record Key \"test1\"]"], + }, + ]); } }); }); diff --git a/scripts/accelerators/string.test.ts b/scripts/accelerators/string.test.ts index d6e69fb..3e169bd 100644 --- a/scripts/accelerators/string.test.ts +++ b/scripts/accelerators/string.test.ts @@ -19,7 +19,13 @@ describe("string type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(". : Input is not a String."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ". : Input is not a String.", + path: [], + }, + ]); } }); @@ -39,7 +45,13 @@ describe("string type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(". : Input String has length less than 2."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ". : Input String has length less than 2.", + path: [], + }, + ]); } }); @@ -59,7 +71,13 @@ describe("string type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(". : Input String has length more than 2."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ". : Input String has length more than 2.", + path: [], + }, + ]); } }); @@ -79,7 +97,13 @@ describe("string type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(". : Input String has length not equal to 2."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ". : Input String has length not equal to 2.", + path: [], + }, + ]); } }); @@ -99,7 +123,13 @@ describe("string type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(". : Input string is not an Email."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ". : Input string is not an Email.", + path: [], + }, + ]); } }); @@ -119,7 +149,13 @@ describe("string type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(". : Input String is not an url."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ". : Input String is not an url.", + path: [], + }, + ]); } }); @@ -139,7 +175,13 @@ describe("string type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(". : Input String is not an emoji."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ". : Input String is not an emoji.", + path: [], + }, + ]); } }); @@ -159,7 +201,13 @@ describe("string type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(". : Input String is not an uuid."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ". : Input String is not an uuid.", + path: [], + }, + ]); } }); @@ -179,7 +227,13 @@ describe("string type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(". : Input String is not an cuid."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ". : Input String is not an cuid.", + path: [], + }, + ]); } }); @@ -199,7 +253,13 @@ describe("string type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(". : Input String is not an cuid2."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ". : Input String is not an cuid2.", + path: [], + }, + ]); } }); @@ -219,7 +279,13 @@ describe("string type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(". : Input String not includes test."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ". : Input String not includes test.", + path: [], + }, + ]); } }); @@ -239,7 +305,13 @@ describe("string type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(". : Input String is not an ulid."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ". : Input String is not an ulid.", + path: [], + }, + ]); } }); @@ -259,7 +331,13 @@ describe("string type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(". : Input String not starts with test."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ". : Input String not starts with test.", + path: [], + }, + ]); } }); @@ -279,7 +357,13 @@ describe("string type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(". : Input String not ends with test."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ". : Input String not ends with test.", + path: [], + }, + ]); } }); @@ -299,7 +383,13 @@ describe("string type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(". : Input String not match with regex test."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ". : Input String not match with regex test.", + path: [], + }, + ]); } }); @@ -343,7 +433,13 @@ describe("string type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(". : Input String is not a datetime."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ". : Input String is not a datetime.", + path: [], + }, + ]); } }); @@ -363,7 +459,13 @@ describe("string type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(". : Input String is not a datetime."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ". : Input String is not a datetime.", + path: [], + }, + ]); } }); @@ -383,7 +485,13 @@ describe("string type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(". : Input String is not a datetime."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ". : Input String is not a datetime.", + path: [], + }, + ]); } }); @@ -403,7 +511,13 @@ describe("string type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(". : Input String is not a datetime."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ". : Input String is not a datetime.", + path: [], + }, + ]); } }); @@ -426,7 +540,13 @@ describe("string type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(". : Input String is not a datetime."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ". : Input String is not a datetime.", + path: [], + }, + ]); } }); @@ -449,7 +569,13 @@ describe("string type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(". : Input String is not a datetime."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ". : Input String is not a datetime.", + path: [], + }, + ]); } }); @@ -473,7 +599,13 @@ describe("string type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(". : Input String is not an ipv4 or ipv6."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ". : Input String is not an ipv4 or ipv6.", + path: [], + }, + ]); } }); @@ -493,7 +625,13 @@ describe("string type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(". : Input String is not an ipv4."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ". : Input String is not an ipv4.", + path: [], + }, + ]); } }); @@ -513,7 +651,13 @@ describe("string type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(". : Input String is not an ipv6."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ". : Input String is not an ipv6.", + path: [], + }, + ]); } }); diff --git a/scripts/accelerators/symbol.test.ts b/scripts/accelerators/symbol.test.ts index 3adb01a..6ccd9a7 100644 --- a/scripts/accelerators/symbol.test.ts +++ b/scripts/accelerators/symbol.test.ts @@ -19,7 +19,13 @@ describe("symbol type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(". : Input is not a Symbol."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ". : Input is not a Symbol.", + path: [], + }, + ]); } }); }); diff --git a/scripts/accelerators/tuple.test.ts b/scripts/accelerators/tuple.test.ts index 17bfaf3..bcd460a 100644 --- a/scripts/accelerators/tuple.test.ts +++ b/scripts/accelerators/tuple.test.ts @@ -19,7 +19,13 @@ describe("tuple type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(".[Tuple 1] : Input is not a Number."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ".[Tuple 1] : Input is not a Number.", + path: ["[Tuple 1]"], + }, + ]); } }); @@ -43,7 +49,13 @@ describe("tuple type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(".[Tuple Rest 2] : Input is not a String."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ".[Tuple Rest 2] : Input is not a String.", + path: ["[Tuple Rest 2]"], + }, + ]); } }); }); diff --git a/scripts/accelerators/type.test.ts b/scripts/accelerators/type.test.ts index e9ce7c9..ceef42f 100644 --- a/scripts/accelerators/type.test.ts +++ b/scripts/accelerators/type.test.ts @@ -21,7 +21,13 @@ describe("type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(". : ZodSchema Fail parse."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ". : ZodSchema Fail parse.", + path: [], + }, + ]); } }); }); diff --git a/scripts/accelerators/undefined.test.ts b/scripts/accelerators/undefined.test.ts index 7a487ba..9af447f 100644 --- a/scripts/accelerators/undefined.test.ts +++ b/scripts/accelerators/undefined.test.ts @@ -20,7 +20,13 @@ describe("undefined type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(". : Input is not Undefined."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ". : Input is not Undefined.", + path: [], + }, + ]); } }); }); diff --git a/scripts/accelerators/union.test.ts b/scripts/accelerators/union.test.ts index 3dec582..a93d728 100644 --- a/scripts/accelerators/union.test.ts +++ b/scripts/accelerators/union.test.ts @@ -37,7 +37,13 @@ describe("union type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(". : Input has no correspondence in union."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ". : Input has no correspondence in union.", + path: [], + }, + ]); } }); }); diff --git a/scripts/accelerators/void.test.ts b/scripts/accelerators/void.test.ts index 6bb36b1..d532f19 100644 --- a/scripts/accelerators/void.test.ts +++ b/scripts/accelerators/void.test.ts @@ -20,7 +20,13 @@ describe("void type", () => { const err: ZodAcceleratorError = error; expect(err).instanceOf(ZodAcceleratorError); expect(schema.safeParse(data).success).toBe(false); - expect(err.message).toBe(". : Input is not Undefined."); + expect(err.issues).toStrictEqual([ + { + code: "custom", + message: ". : Input is not Undefined.", + path: [], + }, + ]); } }); }); diff --git a/scripts/error.ts b/scripts/error.ts index a16c7b2..61a5339 100644 --- a/scripts/error.ts +++ b/scripts/error.ts @@ -1,8 +1,20 @@ -export class ZodAcceleratorError extends Error { +import { ZodError, type ZodIssue } from "zod"; + +export class ZodAcceleratorError extends ZodError { public constructor( - path: string, - message: string, + public passedPath: string, + public passedMessage: string, + issue?: ZodIssue, ) { - super(`${path || "."} : ${message}`); + super([ + { + code: "custom", + message: `${passedPath} : ${passedMessage}`, + path: passedPath === "." + ? [] + : passedPath.substring(1).split("."), + ...issue, + }, + ]); } }