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.
This commit is contained in:
parent
4480be656a
commit
11104dbf29
1 changed files with 9 additions and 1 deletions
|
|
@ -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()
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue