[{"data":1,"prerenderedAt":1058,"buildId":1059},["ShallowReactive",2],{"page:\u002Fextensions\u002Fkubb-labs\u002Fplugin-client\u002Fguides\u002Fvalidate-requests-and-responses":3},{"id":4,"frontmatter":5,"title":6,"heading":6,"body":7,"path":1057,"source":4},"extensions\u002Fkubb-labs\u002Fplugin-client\u002Fguides\u002Fvalidate-requests-and-responses.md",{},"Validate requests and responses",{"type":8,"value":9,"toc":1049},"minimark",[10,35],[11,12,13,14,18,19,22,23,26,27,30,31,34],"p",{},"Pass Zod schemas from ",[15,16,17],"code",{},"@kubb\u002Fplugin-zod"," to your client and run them there. For Valibot, add ",[15,20,21],{},"pluginValibot()"," from ",[15,24,25],{},"@kubb\u002Fplugin-valibot"," and write ",[15,28,29],{},"'valibot'"," wherever this guide writes ",[15,32,33],{},"'zod'",".",[36,37,39,44,62,288,371,374,494,507,511,530,863,874,887,891,898,984,1037],"steps",{"level":38},"2",[40,41,43],"h2",{"id":42},"pass-the-schemas","Pass the schemas",[11,45,46,47,50,51,58,59,61],{},"Add ",[15,48,49],{},"pluginZod()"," and set ",[52,53,55],"a",{"href":54},"https:\u002F\u002Fkubb.dev\u002Fextensions\u002Fkubb-labs\u002Fplugin-client\u002Foptions#validator",[15,56,57],{},"validator",". Without ",[15,60,49],{},", generation stops with an error.",[63,64,69],"pre",{"language":65,"filename":66,"code":67,"class":68},"typescript","kubb.config.ts","import { defineConfig } from 'kubb\u002Fconfig'\nimport { pluginTs } from '@kubb\u002Fplugin-ts'\nimport { pluginZod } from '@kubb\u002Fplugin-zod'\nimport { pluginClient } from '@kubb\u002Fplugin-client'\n\nexport default defineConfig({\n input: '.\u002FpetStore.yaml',\n output: { path: '.\u002Fsrc\u002Fgen' },\n plugins: [\n pluginTs(),\n pluginZod(),\n pluginClient({ importPath: '..\u002F..\u002F..\u002Fclient', validator: 'zod' }),\n ],\n})","highlights",[15,70,72,99,100,99,116,99,132,99,148,99,150,99,165,99,182,99,206,99,218,99,231,99,242,99,275,99,284],{"class":71},"language-typescript",[73,74,77,81,85,89,92,95],"span",{"class":75,"style":76},"line","display: inline",[73,78,80],{"style":79},"color:#d73a49;--hls-dark:#f97583","import ",[73,82,84],{"style":83},"color:#005cc5;--hls-dark:#79b8ff","{ ",[73,86,88],{"style":87},"color:#24292e;--hls-dark:#e1e4e8","defineConfig ",[73,90,91],{"style":83},"} ",[73,93,94],{"style":79},"from ",[73,96,98],{"style":97},"color:#032f62;--hls-dark:#9ecbff","'kubb\u002Fconfig'","\n",[73,101,102,104,106,109,111,113],{"class":75,"style":76},[73,103,80],{"style":79},[73,105,84],{"style":83},[73,107,108],{"style":87},"pluginTs ",[73,110,91],{"style":83},[73,112,94],{"style":79},[73,114,115],{"style":97},"'@kubb\u002Fplugin-ts'",[73,117,118,120,122,125,127,129],{"class":75,"style":76},[73,119,80],{"style":79},[73,121,84],{"style":83},[73,123,124],{"style":87},"pluginZod ",[73,126,91],{"style":83},[73,128,94],{"style":79},[73,130,131],{"style":97},"'@kubb\u002Fplugin-zod'",[73,133,134,136,138,141,143,145],{"class":75,"style":76},[73,135,80],{"style":79},[73,137,84],{"style":83},[73,139,140],{"style":87},"pluginClient ",[73,142,91],{"style":83},[73,144,94],{"style":79},[73,146,147],{"style":97},"'@kubb\u002Fplugin-client'",[73,149],{"class":75,"style":76},[73,151,152,155,158,162],{"class":75,"style":76},[73,153,154],{"style":79},"export ",[73,156,157],{"style":79},"default ",[73,159,161],{"style":160},"color:#6f42c1;--hls-dark:#b392f0","defineConfig",[73,163,164],{"style":83},"({",[73,166,167,170,173,176,179],{"class":75,"style":76},[73,168,169],{"style":83}," ",[73,171,172],{"style":83},"input",[73,174,175],{"style":79},": ",[73,177,178],{"style":97},"'.\u002FpetStore.yaml'",[73,180,181],{"style":79},",",[73,183,184,186,189,191,193,196,198,201,204],{"class":75,"style":76},[73,185,169],{"style":79},[73,187,188],{"style":83},"output",[73,190,175],{"style":79},[73,192,84],{"style":83},[73,194,195],{"style":83},"path",[73,197,175],{"style":79},[73,199,200],{"style":97},"'.\u002Fsrc\u002Fgen' ",[73,202,203],{"style":83},"}",[73,205,181],{"style":79},[73,207,208,210,213,215],{"class":75,"style":76},[73,209,169],{"style":79},[73,211,212],{"style":83},"plugins",[73,214,175],{"style":79},[73,216,217],{"style":83},"[",[73,219,220,223,226,229],{"class":75,"style":76},[73,221,222],{"style":83}," ",[73,224,225],{"style":160},"pluginTs",[73,227,228],{"style":83},"()",[73,230,181],{"style":79},[73,232,233,235,238,240],{"class":75,"style":76},[73,234,222],{"style":79},[73,236,237],{"style":160},"pluginZod",[73,239,228],{"style":83},[73,241,181],{"style":79},[73,243,244,246,249,252,255,257,260,263,265,267,270,273],{"class":75,"style":76},[73,245,222],{"style":79},[73,247,248],{"style":160},"pluginClient",[73,250,251],{"style":83},"({ ",[73,253,254],{"style":83},"importPath",[73,256,175],{"style":79},[73,258,259],{"style":97},"'..\u002F..\u002F..\u002Fclient'",[73,261,262],{"style":79},", ",[73,264,57],{"style":83},[73,266,175],{"style":79},[73,268,269],{"style":97},"'zod' ",[73,271,272],{"style":83},"})",[73,274,181],{"style":79},[73,276,277,279,282],{"class":75,"style":76},[73,278,169],{"style":79},[73,280,281],{"style":83},"]",[73,283,181],{"style":79},[73,285,286],{"class":75,"style":76},[73,287,272],{"style":83},[289,290,291,308],"table",{},[292,293,294],"thead",{},[295,296,297,302],"tr",{},[298,299,300],"th",{},[15,301,57],{},[298,303,304,305],{},"Keys on ",[15,306,307],{},"config.validator",[309,310,311,326,339,351],"tbody",{},[295,312,313,318],{},[314,315,316],"td",{},[15,317,33],{},[314,319,320,262,323],{},[15,321,322],{},"response",[15,324,325],{},"error",[295,327,328,333],{},[314,329,330],{},[15,331,332],{},"{ response: 'zod' }",[314,334,335,262,337],{},[15,336,322],{},[15,338,325],{},[295,340,341,346],{},[314,342,343],{},[15,344,345],{},"{ request: 'zod' }",[314,347,348],{},[15,349,350],{},"request",[295,352,353,358],{},[314,354,355],{},[15,356,357],{},"{ params: 'zod' }",[314,359,360,262,362,262,365,262,368],{},[15,361,195],{},[15,363,364],{},"query",[15,366,367],{},"headers",[15,369,370],{},"cookies",[11,372,373],{},"Each generated call now carries the schemas:",[63,375,377],{"language":65,"code":376,"class":68},"apiClient\u003CAddPetResponses, ThrowOnError>({\n method: 'POST',\n url: '\u002Fpet',\n ...config,\n validator: { response: addPetResponseSchema, error: { 405: addPetStatus405Schema }, ...config.validator },\n})",[15,378,379,99,400,99,414,99,428,99,440,99,490],{"class":71},[73,380,381,384,387,390,392,395,398],{"class":75,"style":76},[73,382,383],{"style":87},"apiClient",[73,385,386],{"style":79},"\u003C",[73,388,389],{"style":160},"AddPetResponses",[73,391,262],{"style":79},[73,393,394],{"style":160},"ThrowOnError",[73,396,397],{"style":79},">",[73,399,164],{"style":83},[73,401,402,404,407,409,412],{"class":75,"style":76},[73,403,169],{"style":83},[73,405,406],{"style":83},"method",[73,408,175],{"style":79},[73,410,411],{"style":97},"'POST'",[73,413,181],{"style":79},[73,415,416,418,421,423,426],{"class":75,"style":76},[73,417,169],{"style":79},[73,419,420],{"style":83},"url",[73,422,175],{"style":79},[73,424,425],{"style":97},"'\u002Fpet'",[73,427,181],{"style":79},[73,429,430,432,435,438],{"class":75,"style":76},[73,431,169],{"style":79},[73,433,434],{"style":79},"...",[73,436,437],{"style":87},"config",[73,439,181],{"style":79},[73,441,442,444,446,448,450,452,454,457,459,461,463,465,468,470,473,475,477,479,481,483,486,488],{"class":75,"style":76},[73,443,169],{"style":79},[73,445,57],{"style":83},[73,447,175],{"style":79},[73,449,84],{"style":83},[73,451,322],{"style":83},[73,453,175],{"style":79},[73,455,456],{"style":87},"addPetResponseSchema",[73,458,262],{"style":79},[73,460,325],{"style":83},[73,462,175],{"style":79},[73,464,84],{"style":83},[73,466,467],{"style":83},"405",[73,469,175],{"style":79},[73,471,472],{"style":87},"addPetStatus405Schema ",[73,474,203],{"style":83},[73,476,262],{"style":79},[73,478,434],{"style":79},[73,480,437],{"style":87},[73,482,34],{"style":79},[73,484,485],{"style":83},"validator ",[73,487,203],{"style":83},[73,489,181],{"style":79},[73,491,492],{"class":75,"style":76},[73,493,272],{"style":83},[11,495,496,497,499,500,503,504,506],{},"A ",[15,498,57],{}," passed on one call merges per key: ",[15,501,502],{},"{ request }"," adds a request schema and keeps the generated response schema. A per-call ",[15,505,325],{}," replaces the whole error map.",[40,508,510],{"id":509},"run-them-in-your-client","Run them in your client",[11,512,513,514,516,517,520,521,524,525,529],{},"Add a ",[15,515,57],{}," field to your ",[15,518,519],{},"RequestConfig"," and run each schema with ",[15,522,523],{},"~standard.validate",", the ",[52,526,528],{"href":527},"https:\u002F\u002Fstandardschema.dev","Standard Schema"," method that Zod implements:",[63,531,534],{"language":65,"filename":532,"code":533,"class":68},"src\u002Fclient.ts","type Result = { value: unknown; issues?: undefined } | { issues: ReadonlyArray\u003Cunknown> }\ntype Schema = { '~standard': { validate: (value: unknown) => Result | Promise\u003CResult> } }\n\nexport type RequestConfig = {\n \u002F\u002F ...\n validator?: { request?: Schema; response?: Schema; error?: Schema | Record\u003Cstring, Schema> }\n}\n\n\u002F\u002F inside client(), after reading the body\nconst result = await config.validator?.response?.['~standard'].validate(body)\nif (result && 'issues' in result && result.issues) throw new Error('Response validation failed')\nconst validatedBody = result && 'value' in result ? result.value : body",[15,535,536,99,594,99,649,99,651,99,665,99,673,99,727,99,731,99,733,99,738,99,782,99,829],{"class":71},[73,537,538,541,544,547,549,552,554,557,560,563,566,568,571,573,576,578,580,582,585,587,589,592],{"class":75,"style":76},[73,539,540],{"style":79},"type ",[73,542,543],{"style":160},"Result ",[73,545,546],{"style":79},"= ",[73,548,84],{"style":83},[73,550,551],{"style":83},"value",[73,553,175],{"style":87},[73,555,556],{"style":160},"unknown",[73,558,559],{"style":79},"; ",[73,561,562],{"style":83},"issues",[73,564,565],{"style":87},"?",[73,567,175],{"style":79},[73,569,570],{"style":83},"undefined ",[73,572,91],{"style":83},[73,574,575],{"style":79},"| ",[73,577,84],{"style":83},[73,579,562],{"style":83},[73,581,175],{"style":87},[73,583,584],{"style":160},"ReadonlyArray",[73,586,386],{"style":79},[73,588,556],{"style":87},[73,590,591],{"style":79},"> ",[73,593,203],{"style":83},[73,595,596,598,601,603,605,608,610,612,615,617,620,622,624,626,629,632,634,636,639,641,644,646],{"class":75,"style":76},[73,597,540],{"style":79},[73,599,600],{"style":160},"Schema ",[73,602,546],{"style":79},[73,604,84],{"style":83},[73,606,607],{"style":97},"'~standard'",[73,609,175],{"style":79},[73,611,84],{"style":83},[73,613,614],{"style":83},"validate",[73,616,175],{"style":79},[73,618,619],{"style":83},"(",[73,621,551],{"style":87},[73,623,175],{"style":87},[73,625,556],{"style":160},[73,627,628],{"style":83},") ",[73,630,631],{"style":79},"=> ",[73,633,543],{"style":160},[73,635,575],{"style":79},[73,637,638],{"style":160},"Promise",[73,640,386],{"style":79},[73,642,643],{"style":160},"Result",[73,645,591],{"style":79},[73,647,648],{"style":83},"} }",[73,650],{"class":75,"style":76},[73,652,653,655,657,660,662],{"class":75,"style":76},[73,654,154],{"style":79},[73,656,540],{"style":79},[73,658,659],{"style":160},"RequestConfig ",[73,661,546],{"style":79},[73,663,664],{"style":83},"{",[73,666,667,669],{"class":75,"style":76},[73,668,169],{"style":83},[73,670,672],{"style":671},"color:#6a737d;--hls-dark:#6a737d","\u002F\u002F ...",[73,674,675,677,679,681,683,685,687,690,693,695,697,699,701,703,705,707,709,711,714,716,719,721,723,725],{"class":75,"style":76},[73,676,169],{"style":671},[73,678,57],{"style":83},[73,680,565],{"style":87},[73,682,175],{"style":79},[73,684,84],{"style":83},[73,686,350],{"style":83},[73,688,689],{"style":87},"?: ",[73,691,692],{"style":160},"Schema",[73,694,559],{"style":79},[73,696,322],{"style":83},[73,698,689],{"style":87},[73,700,692],{"style":160},[73,702,559],{"style":79},[73,704,325],{"style":83},[73,706,689],{"style":87},[73,708,600],{"style":160},[73,710,575],{"style":79},[73,712,713],{"style":160},"Record",[73,715,386],{"style":79},[73,717,718],{"style":87},"string",[73,720,262],{"style":79},[73,722,692],{"style":160},[73,724,591],{"style":79},[73,726,203],{"style":83},[73,728,729],{"class":75,"style":76},[73,730,203],{"style":83},[73,732],{"class":75,"style":76},[73,734,735],{"class":75,"style":76},[73,736,737],{"style":671},"\u002F\u002F inside client(), after reading the body",[73,739,740,743,746,748,751,753,755,757,760,762,764,766,768,770,772,774,776,779],{"class":75,"style":76},[73,741,742],{"style":79},"const ",[73,744,745],{"style":87},"result ",[73,747,546],{"style":79},[73,749,750],{"style":79},"await ",[73,752,437],{"style":87},[73,754,34],{"style":79},[73,756,57],{"style":83},[73,758,759],{"style":79},"?.",[73,761,322],{"style":83},[73,763,759],{"style":79},[73,765,217],{"style":83},[73,767,607],{"style":97},[73,769,281],{"style":83},[73,771,34],{"style":79},[73,773,614],{"style":160},[73,775,619],{"style":83},[73,777,778],{"style":87},"body",[73,780,781],{"style":83},")",[73,783,784,787,789,791,794,797,800,802,804,807,809,811,813,816,819,822,824,827],{"class":75,"style":76},[73,785,786],{"style":79},"if ",[73,788,619],{"style":83},[73,790,745],{"style":87},[73,792,793],{"style":79},"&& ",[73,795,796],{"style":97},"'issues' ",[73,798,799],{"style":79},"in ",[73,801,745],{"style":87},[73,803,793],{"style":79},[73,805,806],{"style":87},"result",[73,808,34],{"style":79},[73,810,562],{"style":83},[73,812,628],{"style":83},[73,814,815],{"style":79},"throw ",[73,817,818],{"style":79},"new ",[73,820,821],{"style":160},"Error",[73,823,619],{"style":83},[73,825,826],{"style":97},"'Response validation failed'",[73,828,781],{"style":83},[73,830,831,833,836,838,840,842,845,847,849,852,854,856,859,861],{"class":75,"style":76},[73,832,742],{"style":79},[73,834,835],{"style":87},"validatedBody ",[73,837,546],{"style":79},[73,839,745],{"style":87},[73,841,793],{"style":79},[73,843,844],{"style":97},"'value' ",[73,846,799],{"style":79},[73,848,745],{"style":87},[73,850,851],{"style":79},"? ",[73,853,806],{"style":87},[73,855,34],{"style":79},[73,857,858],{"style":83},"value ",[73,860,175],{"style":79},[73,862,778],{"style":87},[11,864,865,866,869,870,873],{},"Return ",[15,867,868],{},"validatedBody"," as ",[15,871,872],{},"data",", so a schema that transforms the value takes effect.",[11,875,876,878,879,882,883,886],{},[15,877,325],{}," is keyed by status, such as ",[15,880,881],{},"{ 404: schema, '5xx': schema, default: schema }",". Try the exact status, then its range, then ",[15,884,885],{},"default",". If none matches, skip validation.",[40,888,890],{"id":889},"validate-params","Validate params",[11,892,893,894,897],{},"With ",[15,895,896],{},"validator: { params: 'zod' }",", each call also passes its path, query, header, and cookie schemas:",[63,899,901],{"language":65,"code":900,"class":68},"validator: {\n path: putEventPathSchema,\n query: putEventQuerySchema,\n headers: toHeadersStandardSchema(putEventHeadersSchema, putEventHeadersSchema.shape),\n ...config.validator,\n}",[15,902,903,99,911,99,924,99,937,99,966,99,980],{"class":71},[73,904,905,907,909],{"class":75,"style":76},[73,906,57],{"style":87},[73,908,175],{"style":79},[73,910,664],{"style":83},[73,912,913,915,917,919,922],{"class":75,"style":76},[73,914,169],{"style":83},[73,916,195],{"style":83},[73,918,175],{"style":79},[73,920,921],{"style":87},"putEventPathSchema",[73,923,181],{"style":79},[73,925,926,928,930,932,935],{"class":75,"style":76},[73,927,169],{"style":79},[73,929,364],{"style":83},[73,931,175],{"style":79},[73,933,934],{"style":87},"putEventQuerySchema",[73,936,181],{"style":79},[73,938,939,941,943,945,948,950,953,955,957,959,962,964],{"class":75,"style":76},[73,940,169],{"style":79},[73,942,367],{"style":83},[73,944,175],{"style":79},[73,946,947],{"style":160},"toHeadersStandardSchema",[73,949,619],{"style":83},[73,951,952],{"style":87},"putEventHeadersSchema",[73,954,262],{"style":79},[73,956,952],{"style":87},[73,958,34],{"style":79},[73,960,961],{"style":83},"shape",[73,963,781],{"style":83},[73,965,181],{"style":79},[73,967,968,970,972,974,976,978],{"class":75,"style":76},[73,969,169],{"style":79},[73,971,434],{"style":79},[73,973,437],{"style":87},[73,975,34],{"style":79},[73,977,57],{"style":83},[73,979,181],{"style":79},[73,981,982],{"class":75,"style":76},[73,983,203],{"style":83},[985,986,987,1005,1022,1025],"ol",{},[988,989,46,990,262,992,262,994,996,997,999,1000,1002,1003,34],"li",{},[15,991,195],{},[15,993,364],{},[15,995,367],{},", and ",[15,998,370],{}," to the ",[15,1001,57],{}," field of your ",[15,1004,519],{},[988,1006,1007,1008,262,1011,262,1014,1017,1018,1021],{},"Run each on ",[15,1009,1010],{},"config.path",[15,1012,1013],{},"config.query",[15,1015,1016],{},"config.headers",", or ",[15,1019,1020],{},"config.cookies"," before you build the URL and headers.",[988,1023,1024],{},"Send the values they return. The path and query schemas strip keys the spec does not declare.",[988,1026,1027,1028,1030,1031,1033,1034,34],{},"Export ",[15,1029,947],{}," from your ",[15,1032,254],{}," module. Copy it from ",[15,1035,1036],{},"@kubb\u002Fplugin-client\u002Ftemplates\u002FstandardSchema.ts",[1038,1039,1041,1042,1044,1045,1048],"blockquote",{"as":1040},"warning","If your ",[15,1043,947],{}," returns the schema unchanged, it strips undeclared headers, such as an ",[15,1046,1047],{},"Authorization"," header your client adds. The template keeps them and matches declared headers in any casing.",{"title":1050,"searchDepth":1051,"depth":1051,"links":1052},"",5,[1053,1055,1056],{"id":42,"depth":1054,"text":43},2,{"id":509,"depth":1054,"text":510},{"id":889,"depth":1054,"text":890},"\u002Fextensions\u002Fkubb-labs\u002Fplugin-client\u002Fguides\u002Fvalidate-requests-and-responses",1795727684138,"1b64b1eb-29b7-4f90-ad41-6f24721e1669"]