Skip to content

Commit

Permalink
fix: use the custom error message correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
sunnylost committed Aug 28, 2024
1 parent 8552233 commit 4b94fbc
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
2 changes: 1 addition & 1 deletion deno/lib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ Sponsorship at any level is appreciated and encouraged. If you built a paid prod
<a href="https://neon.tech">
<picture height="68px">
<source media="(prefers-color-scheme: dark)" srcset="https://github.com/user-attachments/assets/83b4b1b1-a9ab-4ae5-a632-56d282f0c444">
<img alt="stainless" height="68px" src="https://github.com/user-attachments/assets/83b4b1b1-a9ab-4ae5-a632-56d282f0c444">
<img alt="Neon" height="68px" src="https://github.com/user-attachments/assets/b5799fc8-81ff-4053-a1c3-b29adf85e7a1">
</picture>
</a>
<br />
Expand Down
9 changes: 6 additions & 3 deletions deno/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,17 @@ function processCreateParams(params: RawCreateParams): ProcessedCreateParams {
const customMap: ZodErrorMap = (iss, ctx) => {
const { message } = params;

if (message != null) {
return { message };
}
if (iss.code === "invalid_enum_value") {
return { message: message ?? ctx.defaultError };
return { message: ctx.defaultError };
}
if (typeof ctx.data === "undefined") {
return { message: message ?? required_error ?? ctx.defaultError };
return { message: required_error ?? ctx.defaultError };
}
if (iss.code !== "invalid_type") return { message: ctx.defaultError };
return { message: message ?? invalid_type_error ?? ctx.defaultError };
return { message: invalid_type_error ?? ctx.defaultError };
};
return { errorMap: customMap, description };
}
Expand Down
9 changes: 6 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,17 @@ function processCreateParams(params: RawCreateParams): ProcessedCreateParams {
const customMap: ZodErrorMap = (iss, ctx) => {
const { message } = params;

if (message != null) {
return { message };
}
if (iss.code === "invalid_enum_value") {
return { message: message ?? ctx.defaultError };
return { message: ctx.defaultError };
}
if (typeof ctx.data === "undefined") {
return { message: message ?? required_error ?? ctx.defaultError };
return { message: required_error ?? ctx.defaultError };
}
if (iss.code !== "invalid_type") return { message: ctx.defaultError };
return { message: message ?? invalid_type_error ?? ctx.defaultError };
return { message: invalid_type_error ?? ctx.defaultError };
};
return { errorMap: customMap, description };
}
Expand Down

0 comments on commit 4b94fbc

Please sign in to comment.