Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(47): rework error #48

Merged
merged 1 commit into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions scripts/accelerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
40 changes: 35 additions & 5 deletions scripts/accelerators/array.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]"],
},
]);
}
});

Expand All @@ -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"],
},
]);
}
});

Expand All @@ -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: [],
},
]);
}
});

Expand All @@ -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: [],
},
]);
}
});

Expand All @@ -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: [],
},
]);
}
});
});
41 changes: 35 additions & 6 deletions scripts/accelerators/bigInt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [],
},
]);
}
});

Expand All @@ -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: [],
},
]);
}
});

Expand All @@ -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: [],
},
]);
}
});

Expand All @@ -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: [],
},
]);
}
});

Expand All @@ -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: [],
},
]);
}
});
});
8 changes: 7 additions & 1 deletion scripts/accelerators/boolean.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [],
},
]);
}
});

Expand Down
32 changes: 28 additions & 4 deletions scripts/accelerators/date.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [],
},
]);
}
});

Expand All @@ -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: [],
},
]);
}
});

Expand All @@ -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: [],
},
]);
}
});

Expand All @@ -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: [],
},
]);
}
});
});
8 changes: 7 additions & 1 deletion scripts/accelerators/default.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [],
},
]);
}
});

Expand Down
32 changes: 28 additions & 4 deletions scripts/accelerators/effects.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [],
},
]);
}
});

Expand All @@ -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: [],
},
]);
}
});

Expand Down Expand Up @@ -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: [],
},
]);
}
});

Expand Down Expand Up @@ -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: [],
},
]);
}
});
});
39 changes: 30 additions & 9 deletions scripts/accelerators/effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => `
Expand All @@ -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) => `
Expand All @@ -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.")};
}
`,
};
Expand Down
Loading
Loading