From b15f725ead981387f80f089d0523d9c2748b184e Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Fri, 6 Sep 2024 10:38:37 -0400 Subject: [PATCH] Prevent Zod errors from crashing build (#2288) --- .changeset/famous-glasses-clean.md | 7 +++++++ packages/starlight/utils/error-map.ts | 3 ++- 2 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 .changeset/famous-glasses-clean.md diff --git a/.changeset/famous-glasses-clean.md b/.changeset/famous-glasses-clean.md new file mode 100644 index 0000000000..b5749f54a2 --- /dev/null +++ b/.changeset/famous-glasses-clean.md @@ -0,0 +1,7 @@ +--- +'@astrojs/starlight': patch +--- + +Safely handle Zod errors + +Prevents bugs where errors without the `.received` props would through and cause builds to fail unnecessarily. diff --git a/packages/starlight/utils/error-map.ts b/packages/starlight/utils/error-map.ts index 667a91af36..9c7475a096 100644 --- a/packages/starlight/utils/error-map.ts +++ b/packages/starlight/utils/error-map.ts @@ -143,7 +143,8 @@ const errorMap: z.ZodErrorMap = (baseError, ctx) => { }; const getTypeOrLiteralMsg = (error: TypeOrLiteralErrByPathEntry): string => { - if (error.received === 'undefined') return 'Required'; + // received could be `undefined` or the string `'undefined'` + if (typeof error.received === 'undefined' || error.received === 'undefined') return 'Required'; const expectedDeduped = new Set(error.expected); switch (error.code) { case 'invalid_type':