From 4b94fbc8a9c7ed35dbeeba672147f8e04fbac92a Mon Sep 17 00:00:00 2001 From: sunnylost Date: Wed, 28 Aug 2024 18:51:23 +0800 Subject: [PATCH] fix: use the custom error message correctly --- deno/lib/README.md | 2 +- deno/lib/types.ts | 9 ++++++--- src/types.ts | 9 ++++++--- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/deno/lib/README.md b/deno/lib/README.md index ea8ce8b34..c461b637a 100644 --- a/deno/lib/README.md +++ b/deno/lib/README.md @@ -219,7 +219,7 @@ Sponsorship at any level is appreciated and encouraged. If you built a paid prod - stainless + Neon
diff --git a/deno/lib/types.ts b/deno/lib/types.ts index d634e524f..441a3c8cf 100644 --- a/deno/lib/types.ts +++ b/deno/lib/types.ts @@ -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 }; } diff --git a/src/types.ts b/src/types.ts index 0767073c5..ba42f7ddf 100644 --- a/src/types.ts +++ b/src/types.ts @@ -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 }; }