[{"data":1,"prerenderedAt":457},["ShallowReactive",2],{"page:\u002Fextensions\u002Fkubb-labs\u002Fplugin-zod\u002Fguides\u002Fcoercion":3},{"id":4,"frontmatter":5,"title":6,"heading":6,"body":7,"path":456,"source":4},"extensions\u002Fkubb-labs\u002Fplugin-zod\u002Fguides\u002Fcoercion.md",{},"Coerce input with Zod",{"type":8,"value":9,"toc":447},"minimark",[10,23,28,41,186,262,266,274,321,325,337,389,393,407,434],[11,12,13,14,18,19,22],"p",{},"Set ",[15,16,17],"code",{},"coercion"," to convert form data and query strings with ",[15,20,21],{},"z.coerce"," before Zod validates them, and see which format checks each key turns off.",[24,25,27],"h2",{"id":26},"turn-on-coercion","Turn on coercion",[11,29,30,31,34,35,40],{},"Pass ",[15,32,33],{},"true"," to coerce strings, numbers, and dates, or an object to pick the primitives. See ",[36,37,39],"a",{"href":38},"https:\u002F\u002Fzod.dev\u002F?id=coercion-for-primitives","Coercion for primitives"," in the Zod docs.",[42,43,44,111,146],"code-group",{},[45,46,51],"pre",{"language":47,"filename":48,"code":49,"class":50},"typescript","kubb.config.ts","import { pluginZod } from '@kubb\u002Fplugin-zod'\n\npluginZod({ coercion: { numbers: true } })","highlights",[15,52,54,81,82,81,84],{"class":53},"language-typescript",[55,56,59,63,67,71,74,77],"span",{"class":57,"style":58},"line","display: inline",[55,60,62],{"style":61},"color:#d73a49;--hls-dark:#f97583","import ",[55,64,66],{"style":65},"color:#005cc5;--hls-dark:#79b8ff","{ ",[55,68,70],{"style":69},"color:#24292e;--hls-dark:#e1e4e8","pluginZod ",[55,72,73],{"style":65},"} ",[55,75,76],{"style":61},"from ",[55,78,80],{"style":79},"color:#032f62;--hls-dark:#9ecbff","'@kubb\u002Fplugin-zod'","\n",[55,83],{"class":57,"style":58},[55,85,86,90,93,95,98,100,103,105,108],{"class":57,"style":58},[55,87,89],{"style":88},"color:#6f42c1;--hls-dark:#b392f0","pluginZod",[55,91,92],{"style":65},"({ ",[55,94,17],{"style":65},[55,96,97],{"style":61},": ",[55,99,66],{"style":65},[55,101,102],{"style":65},"numbers",[55,104,97],{"style":61},[55,106,107],{"style":65},"true ",[55,109,110],{"style":65},"} })",[45,112,115],{"language":47,"filename":113,"code":114,"class":50},"Before","limit: z.int().optional(),",[15,116,117],{"class":53},[55,118,119,122,124,127,130,133,136,138,141,143],{"class":57,"style":58},[55,120,121],{"style":69},"limit",[55,123,97],{"style":61},[55,125,126],{"style":69},"z",[55,128,129],{"style":61},".",[55,131,132],{"style":88},"int",[55,134,135],{"style":65},"()",[55,137,129],{"style":61},[55,139,140],{"style":88},"optional",[55,142,135],{"style":65},[55,144,145],{"style":61},",",[45,147,150],{"language":47,"filename":148,"code":149,"class":50},"After","limit: z.coerce.number().int().optional(),",[15,151,152],{"class":53},[55,153,154,156,158,160,162,165,167,170,172,174,176,178,180,182,184],{"class":57,"style":58},[55,155,121],{"style":69},[55,157,97],{"style":61},[55,159,126],{"style":69},[55,161,129],{"style":61},[55,163,164],{"style":65},"coerce",[55,166,129],{"style":61},[55,168,169],{"style":88},"number",[55,171,135],{"style":65},[55,173,129],{"style":61},[55,175,132],{"style":88},[55,177,135],{"style":65},[55,179,129],{"style":61},[55,181,140],{"style":88},[55,183,135],{"style":65},[55,185,145],{"style":61},[187,188,189,202],"table",{},[190,191,192],"thead",{},[193,194,195,199],"tr",{},[196,197,198],"th",{},"Value",[196,200,201],{},"Effect",[203,204,205,232,252],"tbody",{},[193,206,207,214],{},[208,209,210,213],"td",{},[15,211,212],{},"false"," (default)",[208,215,216,217,220,221,224,225,228,229,129],{},"No coercion, except ",[15,218,219],{},"format: int64"," fields, which use ",[15,222,223],{},"z.coerce.bigint()"," because JSON holds a number. With the adapter's ",[15,226,227],{},"integerType: 'number'",", they print ",[15,230,231],{},"z.int()",[193,233,234,238],{},[208,235,236],{},[15,237,33],{},[208,239,240,241,244,245,248,249,129],{},"Coerces strings, numbers, and dates with ",[15,242,243],{},"z.coerce.string()",", ",[15,246,247],{},"z.coerce.number()",", and ",[15,250,251],{},"z.coerce.date()",[193,253,254,259],{},[208,255,256],{},[15,257,258],{},"{ dates?, strings?, numbers? }",[208,260,261],{},"Coerces only the primitives you pick.",[24,263,265],{"id":264},"coerce-strings","Coerce strings",[11,267,268,271,272,129],{},[15,269,270],{},"strings"," prints string fields as ",[15,273,243],{},[275,276,277,303],"ul",{},[278,279,280,281,244,284,244,287,244,290,244,293,244,296,248,299,302],"li",{},"It replaces the ",[15,282,283],{},"base64",[15,285,286],{},"base64url",[15,288,289],{},"jwt",[15,291,292],{},"ulid",[15,294,295],{},"iban",[15,297,298],{},"duration",[15,300,301],{},"hostname"," validators, so Zod no longer checks those formats.",[278,304,305,244,308,244,311,244,314,248,317,320],{},[15,306,307],{},"email",[15,309,310],{},"uri",[15,312,313],{},"uuid",[15,315,316],{},"ipv4",[15,318,319],{},"ipv6"," keep their validator and are not coerced.",[24,322,324],{"id":323},"coerce-numbers","Coerce numbers",[11,326,327,329,330,333,334,129],{},[15,328,102],{}," coerces number and integer fields, and enums with a numeric ",[15,331,332],{},"format"," such as ",[15,335,336],{},"int32",[275,338,339,360,371,386],{},[278,340,341,342,344,345,348,349,352,353,355,356,359],{},"Integers typed as ",[15,343,169],{}," print as ",[15,346,347],{},"z.coerce.number().int()",", or ",[15,350,351],{},"z.coerce.number().check(z.int())"," with Zod Mini. The coerced call drops the ",[15,354,336],{}," and ",[15,357,358],{},"uint32"," range checks.",[278,361,362,364,365,368,369,129],{},[15,363,219],{}," fields typed as ",[15,366,367],{},"bigint",", the adapter default, keep ",[15,370,223],{},[278,372,373,374,377,378,381,382,385],{},"When a coerced enum is the discriminator of a ",[15,375,376],{},"oneOf",", the union prints as ",[15,379,380],{},"z.union",", because ",[15,383,384],{},"z.discriminatedUnion"," cannot read coerced values.",[278,387,388],{},"Other enums are not coerced.",[24,390,392],{"id":391},"coerce-dates","Coerce dates",[11,394,395,398,399,402,403,406],{},[15,396,397],{},"dates"," coerces only fields typed as ",[15,400,401],{},"Date",", which needs ",[15,404,405],{},"dateType: 'date'"," on the adapter.",[275,408,409,412],{},[278,410,411],{},"Fields kept as ISO strings are never coerced.",[278,413,414,417,418,421,422,425,426,429,430,129],{},[15,415,416],{},"format: time"," fields are never coerced, because ",[15,419,420],{},"new Date()"," cannot parse a bare ",[15,423,424],{},"HH:mm:ss",". For a time-of-day type such as ",[15,427,428],{},"Temporal.PlainTime",", see ",[36,431,433],{"href":432},"https:\u002F\u002Fkubb.dev\u002Fextensions\u002Fkubb-labs\u002Fplugin-zod\u002Fguides\u002Fcustomization#encode-custom-types","Encode custom types",[435,436,438,439,442,443,129],"blockquote",{"as":437},"note","Coerced fields run on Zod's interpreter when you turn on ",[15,440,441],{},"compile",". See ",[36,444,446],{"href":445},"https:\u002F\u002Fkubb.dev\u002Fextensions\u002Fkubb-labs\u002Fplugin-zod\u002Fguides\u002Fcompiled-schemas#coerced-and-int64-fields","Compile Zod schemas",{"title":448,"searchDepth":449,"depth":449,"links":450},"",5,[451,453,454,455],{"id":26,"depth":452,"text":27},2,{"id":264,"depth":452,"text":265},{"id":323,"depth":452,"text":324},{"id":391,"depth":452,"text":392},"\u002Fextensions\u002Fkubb-labs\u002Fplugin-zod\u002Fguides\u002Fcoercion",1794776439316]