From 11104dbf298d359ee048c4824de305b385678a95 Mon Sep 17 00:00:00 2001 From: Chever John Date: Thu, 21 Aug 2025 15:46:00 +0800 Subject: [PATCH] Improves boolean parsing for 'purchased' field Updates the StoreItemSchema to handle various input formats for the 'purchased' field, including strings and numbers. This ensures that the application can correctly interpret boolean values from different sources, preventing data validation errors and improving the user experience. Adds a preprocess function to the schema to convert string values like "on", "true", "1", "off", "false", and "0", and number values of 1 and 0 to their respective boolean equivalents before validation. --- server/src/index.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/server/src/index.ts b/server/src/index.ts index 91e1177..fb1781e 100644 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -44,7 +44,15 @@ const StoreItemSchema = z.object({ category: z.string(), site: z.string(), batch: z.string(), - purchased: z.boolean(), + purchased: z.preprocess((v) => { + if (typeof v === 'string') { + const s = v.toLowerCase(); + if (s === 'on' || s === 'true' || s === '1') return true; + if (s === 'off' || s === 'false' || s === '0') return false; + } + if (typeof v === 'number') return v === 1 ? true : v === 0 ? false : v; + return v; + }, z.boolean()), weight: z.number(), purchaseLink: z.string().optional() });