[{"data":1,"prerenderedAt":305},["ShallowReactive",2],{"\u002Fnotes\u002Ffiltered-array-schema":3},{"id":4,"title":5,"body":6,"description":15,"draft":296,"extension":297,"meta":298,"navigation":299,"path":300,"publishDate":301,"seo":302,"stem":303,"__hash__":304},"notes\u002Fnotes\u002Ffiltered-array-schema.md","Zod filtered array schema",{"type":7,"value":8,"toc":294},"minimark",[9,290],[10,11,16],"pre",{"className":12,"code":13,"language":14,"meta":15,"style":15},"language-ts shiki shiki-themes github-light github-light github-dark","\u002F**\n * Creates a Zod schema for an array that filters out invalid elements during preprocessing.\n *\n * @template T - Type extending ZodSchema\n * @param {T} schema - The Zod schema to validate individual array elements\n * @returns {ZodEffects\u003CZodArray\u003CT>>} A new Zod schema that:\n * 1. Converts non-array inputs into single-element arrays\n * 2. Filters out elements that don't match the provided schema\n * 3. Validates the resulting array against the schema\n *\n * @example\n * const numberSchema = makeFilteredArraySchema(z.number());\n * numberSchema.parse(['1', 2, '3', 4]); \u002F\u002F Returns [2, 4]\n *\u002F\nfunction makeFilteredArraySchema\u003CT extends ZodSchema>(schema: T) {\n  return z.preprocess((val) => {\n    const array = Array.isArray(val) ? val : [val];\n    return array.filter((item: unknown) => schema.safeParse(item).success);\n  }, z.array(schema));\n}\n","ts","",[17,18,19,28,34,40,57,75,89,95,101,107,112,120,126,135,141,177,204,237,272,284],"code",{"__ignoreMap":15},[20,21,24],"span",{"class":22,"line":23},"line",1,[20,25,27],{"style":26},"--shiki-light:#6A737D;--shiki-default:#6A737D;--shiki-dark:#6A737D","\u002F**\n",[20,29,31],{"class":22,"line":30},2,[20,32,33],{"style":26}," * Creates a Zod schema for an array that filters out invalid elements during preprocessing.\n",[20,35,37],{"class":22,"line":36},3,[20,38,39],{"style":26}," *\n",[20,41,43,46,50,54],{"class":22,"line":42},4,[20,44,45],{"style":26}," * ",[20,47,49],{"style":48},"--shiki-light:#D73A49;--shiki-default:#D73A49;--shiki-dark:#F97583","@template",[20,51,53],{"style":52},"--shiki-light:#24292E;--shiki-default:#24292E;--shiki-dark:#E1E4E8"," T",[20,55,56],{"style":26}," - Type extending ZodSchema\n",[20,58,60,62,65,69,72],{"class":22,"line":59},5,[20,61,45],{"style":26},[20,63,64],{"style":48},"@param",[20,66,68],{"style":67},"--shiki-light:#6F42C1;--shiki-default:#6F42C1;--shiki-dark:#B392F0"," {T}",[20,70,71],{"style":52}," schema",[20,73,74],{"style":26}," - The Zod schema to validate individual array elements\n",[20,76,78,80,83,86],{"class":22,"line":77},6,[20,79,45],{"style":26},[20,81,82],{"style":48},"@returns",[20,84,85],{"style":67}," {ZodEffects\u003CZodArray\u003CT>>}",[20,87,88],{"style":26}," A new Zod schema that:\n",[20,90,92],{"class":22,"line":91},7,[20,93,94],{"style":26}," * 1. Converts non-array inputs into single-element arrays\n",[20,96,98],{"class":22,"line":97},8,[20,99,100],{"style":26}," * 2. Filters out elements that don't match the provided schema\n",[20,102,104],{"class":22,"line":103},9,[20,105,106],{"style":26}," * 3. Validates the resulting array against the schema\n",[20,108,110],{"class":22,"line":109},10,[20,111,39],{"style":26},[20,113,115,117],{"class":22,"line":114},11,[20,116,45],{"style":26},[20,118,119],{"style":48},"@example\n",[20,121,123],{"class":22,"line":122},12,[20,124,125],{"style":26}," * const numberSchema = makeFilteredArraySchema(z.number());\n",[20,127,129,132],{"class":22,"line":128},13,[20,130,131],{"style":26}," * numberSchema.parse(['1', 2, '3', 4]);",[20,133,134],{"style":26}," \u002F\u002F Returns [2, 4]\n",[20,136,138],{"class":22,"line":137},14,[20,139,140],{"style":26}," *\u002F\n",[20,142,144,147,150,153,156,159,162,165,169,172,174],{"class":22,"line":143},15,[20,145,146],{"style":48},"function",[20,148,149],{"style":67}," makeFilteredArraySchema",[20,151,152],{"style":52},"\u003C",[20,154,155],{"style":67},"T",[20,157,158],{"style":48}," extends",[20,160,161],{"style":67}," ZodSchema",[20,163,164],{"style":52},">(",[20,166,168],{"style":167},"--shiki-light:#E36209;--shiki-default:#E36209;--shiki-dark:#FFAB70","schema",[20,170,171],{"style":48},":",[20,173,53],{"style":67},[20,175,176],{"style":52},") {\n",[20,178,180,183,186,189,192,195,198,201],{"class":22,"line":179},16,[20,181,182],{"style":48},"  return",[20,184,185],{"style":52}," z.",[20,187,188],{"style":67},"preprocess",[20,190,191],{"style":52},"((",[20,193,194],{"style":167},"val",[20,196,197],{"style":52},") ",[20,199,200],{"style":48},"=>",[20,202,203],{"style":52}," {\n",[20,205,207,210,214,217,220,223,226,229,232,234],{"class":22,"line":206},17,[20,208,209],{"style":48},"    const",[20,211,213],{"style":212},"--shiki-light:#005CC5;--shiki-default:#005CC5;--shiki-dark:#79B8FF"," array",[20,215,216],{"style":48}," =",[20,218,219],{"style":52}," Array.",[20,221,222],{"style":67},"isArray",[20,224,225],{"style":52},"(val) ",[20,227,228],{"style":48},"?",[20,230,231],{"style":52}," val ",[20,233,171],{"style":48},[20,235,236],{"style":52}," [val];\n",[20,238,240,243,246,249,251,254,256,259,261,263,266,269],{"class":22,"line":239},18,[20,241,242],{"style":48},"    return",[20,244,245],{"style":52}," array.",[20,247,248],{"style":67},"filter",[20,250,191],{"style":52},[20,252,253],{"style":167},"item",[20,255,171],{"style":48},[20,257,258],{"style":212}," unknown",[20,260,197],{"style":52},[20,262,200],{"style":48},[20,264,265],{"style":52}," schema.",[20,267,268],{"style":67},"safeParse",[20,270,271],{"style":52},"(item).success);\n",[20,273,275,278,281],{"class":22,"line":274},19,[20,276,277],{"style":52},"  }, z.",[20,279,280],{"style":67},"array",[20,282,283],{"style":52},"(schema));\n",[20,285,287],{"class":22,"line":286},20,[20,288,289],{"style":52},"}\n",[291,292,293],"style",{},"html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":15,"searchDepth":30,"depth":30,"links":295},[],false,"md",{},true,"\u002Fnotes\u002Ffiltered-array-schema","2025-04-08",{"title":5,"description":15},"notes\u002Ffiltered-array-schema","jEcMFAVXCbsijOTFj2xMLAO7tqSCeUDAP8BGbQhvQ00",1793618030821]