[{"data":1,"prerenderedAt":951},["ShallowReactive",2],{"$fezBzPSfU4v3celWB2X2HUBEDLODnnGxsTJdcesaHZBo":3},{"slug":4,"title":5,"description":6,"source":7,"document":8,"toc":932,"editUrl":945,"prev":946,"next":949},"overview\u002Fpipes","Pipes","Transform and validate handler inputs with the Pipe trait, ParseIntPipe and DefaultValuePipe.","---\ntitle: Pipes\ndescription: Transform and validate handler inputs with the Pipe trait, ParseIntPipe and DefaultValuePipe.\n---\n\n# Pipes\n\nPipes run just before the handler and **transform or validate** the inputs it receives. A pipe takes a value of one type, transforms it, and either returns a new value or aborts with a `PipeError`.\n\n## The `Pipe` trait\n\n```rust\npub trait Pipe\u003CInput> {\n    type Output;\n\n    fn transform(&self, value: Input) -> Result\u003CSelf::Output, PipeError>;\n}\n```\n\n## Built-in pipes\n\n### `ParseIntPipe`\n\nParses a `String` into an `i64`:\n\n```rust\nuse ferrite_framework::Pipe;\n\nlet pipe = ParseIntPipe;\nlet n: i64 = pipe.transform(\"42\".to_string())?;   \u002F\u002F Ok(42)\npipe.transform(\"abc\".to_string())                 \u002F\u002F Err(PipeError)\n```\n\nUse it in error paths instead of manual `.parse().map_err(...)`:\n\n```rust\nuse ferrite_framework::{HttpError, Pipe, ParseIntPipe};\n\nlet id = ParseIntPipe\n    .transform(raw_id)\n    .map_err(|_| HttpError::bad_request(\"id must be an integer\"))?;\n```\n\n### `DefaultValuePipe`\n\nReturns a default string when the input is empty:\n\n```rust\nuse ferrite_framework::{Pipe, DefaultValuePipe};\n\nlet pipe = DefaultValuePipe(\"development\".to_string());\nlet env = pipe.transform(\"\".to_string())?;   \u002F\u002F \"development\"\nlet env = pipe.transform(\"prod\".to_string())?; \u002F\u002F \"prod\"\n```\n\n## Custom pipes\n\nImplement the trait on any value:\n\n```rust\nuse ferrite_framework::{Pipe, PipeError};\n\npub struct SlugPipe;\n\nimpl Pipe\u003CString> for SlugPipe {\n    type Output = String;\n\n    fn transform(&self, value: String) -> Result\u003CSelf::Output, PipeError> {\n        if value.trim().is_empty() {\n            return Err(PipeError::bad_request(\"slug must not be empty\"));\n        }\n        Ok(value.trim().to_lowercase().replace(' ', \"-\"))\n    }\n}\n```\n\n### `PipeError`\n\n```rust\nlet err = PipeError::new(\"message\");\nlet err = PipeError::response(response);  \u002F\u002F wrap an arbitrary Response\nlet err = PipeError::bad_request(\"...\"):  \u002F\u002F shorthand constructors\nlet err = PipeError::forbidden(\"...\");\nlet err = PipeError::not_found(\"...\");\n```\n\n`PipeError::bad_request` renders as a 400; `PipeError::response` lets you short-circuit with a fully-formed response.\n\n## Validation pipes\n\nThe validation crate ships `ValidationPipe\u003CRhs?>` ([source](\u002Fdocs\u002Foverview\u002Fvalidation)): it runs `Validate` on the value and maps errors to a `PipeError::response(422)` carrying `ValidationErrors`. It integrates with the validating `ferrite_framework::Json\u003CT>` extractor, which validates automatically on deserialization.\n\n## Pipeline position\n\n```text\nMiddleware → Guards → Interceptors(before) → Pipes → Handler\n```\n\nA pipe failure aborts the pipeline and (if applicable) passes through registered exception filters.\n\n## Next steps\n\n- [Validation](\u002Fdocs\u002Foverview\u002Fvalidation).\n- [Exception filters](\u002Fdocs\u002Foverview\u002Fexception-filters).",{"frontmatter":9,"meta":10,"nodes":45},{"title":5,"description":6},{"toc":11},{"title":12,"searchDepth":13,"depth":14,"links":15},"On This Page",2,3,[16,19,29,36,39,42],{"id":17,"depth":13,"text":18},"the-pipe-trait","The Pipe trait",{"id":20,"depth":13,"text":21,"children":22},"built-in-pipes","Built-in pipes",[23,26],{"id":24,"depth":14,"text":25},"built-in-pipes-parseintpipe","ParseIntPipe",{"id":27,"depth":14,"text":28},"built-in-pipes-defaultvaluepipe","DefaultValuePipe",{"id":30,"depth":13,"text":31,"children":32},"custom-pipes","Custom pipes",[33],{"id":34,"depth":14,"text":35},"custom-pipes-pipeerror","PipeError",{"id":37,"depth":13,"text":38},"validation-pipes","Validation pipes",{"id":40,"depth":13,"text":41},"pipeline-position","Pipeline position",{"id":43,"depth":13,"text":44},"next-steps","Next steps",[46,50,63,71,178,180,185,195,302,308,397,401,404,517,519,522,735,739,858,868,870,899,901,912,915,917],[47,48,5],"h1",{"id":49},"pipes",[51,52,53,54,58,59,62],"p",{},"Pipes run just before the handler and ",[55,56,57],"strong",{},"transform or validate"," the inputs it receives. A pipe takes a value of one type, transforms it, and either returns a new value or aborts with a ",[60,61,35],"code",{},".",[64,65,66,67,70],"h2",{"id":17},"The ",[60,68,69],{},"Pipe"," trait",[72,73,76],"pre",{"language":74,"class":75},"rust","shiki shiki-themes github-light github-dark dark:github-dark",[60,77,79,104,105,104,116,104,118,104,173],{"class":78},"language-rust",[80,81,84,88,90,94,98,101],"span",{"class":82,"style":83},"line","display: inline",[80,85,87],{"style":86},"color:#D73A49;--shiki-dark:#F97583","pub",[80,89,70],{"style":86},[80,91,93],{"style":92},"color:#6F42C1;--shiki-dark:#B392F0"," Pipe",[80,95,97],{"style":96},"color:#24292E;--shiki-dark:#E1E4E8","\u003C",[80,99,100],{"style":92},"Input",[80,102,103],{"style":96},"> {","\n",[80,106,107,110,113],{"class":82,"style":83},[80,108,109],{"style":86},"    type",[80,111,112],{"style":92}," Output",[80,114,115],{"style":96},";",[80,117],{"class":82,"style":83},[80,119,120,123,126,129,132,136,139,142,145,148,151,154,156,159,162,165,168,170],{"class":82,"style":83},[80,121,122],{"style":86},"    fn",[80,124,125],{"style":92}," transform",[80,127,128],{"style":96},"(",[80,130,131],{"style":86},"&",[80,133,135],{"style":134},"color:#005CC5;--shiki-dark:#79B8FF","self",[80,137,138],{"style":96},", value",[80,140,141],{"style":86},":",[80,143,144],{"style":92}," Input",[80,146,147],{"style":96},") ",[80,149,150],{"style":86},"->",[80,152,153],{"style":92}," Result",[80,155,97],{"style":96},[80,157,158],{"style":134},"Self",[80,160,161],{"style":86},"::",[80,163,164],{"style":92},"Output",[80,166,167],{"style":96},", ",[80,169,35],{"style":92},[80,171,172],{"style":96},">;",[80,174,175],{"class":82,"style":83},[80,176,177],{"style":96},"}",[64,179,21],{"id":20},[181,182,183],"h3",{"id":24},[60,184,25],{},[51,186,187,188,191,192,141],{},"Parses a ",[60,189,190],{},"String"," into an ",[60,193,194],{},"i64",[72,196,197],{"language":74,"class":75},[60,198,199,104,213,104,215,104,231,104,278],{"class":78},[80,200,201,204,207,209,211],{"class":82,"style":83},[80,202,203],{"style":86},"use",[80,205,206],{"style":92}," ferrite_framework",[80,208,161],{"style":86},[80,210,69],{"style":92},[80,212,115],{"style":96},[80,214],{"class":82,"style":83},[80,216,217,220,223,226,229],{"class":82,"style":83},[80,218,219],{"style":86},"let",[80,221,222],{"style":96}," pipe ",[80,224,225],{"style":86},"=",[80,227,228],{"style":92}," ParseIntPipe",[80,230,115],{"style":96},[80,232,233,235,238,240,243,246,249,251,254,256,260,262,265,268,271,274],{"class":82,"style":83},[80,234,219],{"style":86},[80,236,237],{"style":96}," n",[80,239,141],{"style":86},[80,241,242],{"style":92}," i64",[80,244,245],{"style":86}," =",[80,247,248],{"style":96}," pipe",[80,250,62],{"style":86},[80,252,253],{"style":92},"transform",[80,255,128],{"style":96},[80,257,259],{"style":258},"color:#032F62;--shiki-dark:#9ECBFF","\"42\"",[80,261,62],{"style":86},[80,263,264],{"style":92},"to_string",[80,266,267],{"style":96},"())",[80,269,270],{"style":86},"?",[80,272,273],{"style":96},";   ",[80,275,277],{"style":276},"color:#6A737D;--shiki-dark:#6A737D","\u002F\u002F Ok(42)",[80,279,280,283,285,287,289,292,294,296,299],{"class":82,"style":83},[80,281,282],{"style":96},"pipe",[80,284,62],{"style":86},[80,286,253],{"style":92},[80,288,128],{"style":96},[80,290,291],{"style":258},"\"abc\"",[80,293,62],{"style":86},[80,295,264],{"style":92},[80,297,298],{"style":96},"())                 ",[80,300,301],{"style":276},"\u002F\u002F Err(PipeError)",[51,303,304,305,141],{},"Use it in error paths instead of manual ",[60,306,307],{},".parse().map_err(...)",[72,309,310],{"language":74,"class":75},[60,311,312,104,337,104,339,104,350,104,360],{"class":78},[80,313,314,316,318,320,323,326,328,330,332,334],{"class":82,"style":83},[80,315,203],{"style":86},[80,317,206],{"style":92},[80,319,161],{"style":86},[80,321,322],{"style":96},"{",[80,324,325],{"style":92},"HttpError",[80,327,167],{"style":96},[80,329,69],{"style":92},[80,331,167],{"style":96},[80,333,25],{"style":92},[80,335,336],{"style":96},"};",[80,338],{"class":82,"style":83},[80,340,341,343,346,348],{"class":82,"style":83},[80,342,219],{"style":86},[80,344,345],{"style":96}," id ",[80,347,225],{"style":86},[80,349,228],{"style":92},[80,351,352,355,357],{"class":82,"style":83},[80,353,354],{"style":86},"    .",[80,356,253],{"style":92},[80,358,359],{"style":96},"(raw_id)",[80,361,362,364,367,369,372,375,377,380,382,385,387,390,393,395],{"class":82,"style":83},[80,363,354],{"style":86},[80,365,366],{"style":92},"map_err",[80,368,128],{"style":96},[80,370,371],{"style":86},"|",[80,373,374],{"style":96},"_",[80,376,371],{"style":86},[80,378,379],{"style":92}," HttpError",[80,381,161],{"style":86},[80,383,384],{"style":92},"bad_request",[80,386,128],{"style":96},[80,388,389],{"style":258},"\"id must be an integer\"",[80,391,392],{"style":96},"))",[80,394,270],{"style":86},[80,396,115],{"style":96},[181,398,399],{"id":27},[60,400,28],{},[51,402,403],{},"Returns a default string when the input is empty:",[72,405,406],{"language":74,"class":75},[60,407,408,104,426,104,428,104,451,104,484],{"class":78},[80,409,410,412,414,416,418,420,422,424],{"class":82,"style":83},[80,411,203],{"style":86},[80,413,206],{"style":92},[80,415,161],{"style":86},[80,417,322],{"style":96},[80,419,69],{"style":92},[80,421,167],{"style":96},[80,423,28],{"style":92},[80,425,336],{"style":96},[80,427],{"class":82,"style":83},[80,429,430,432,434,436,439,441,444,446,448],{"class":82,"style":83},[80,431,219],{"style":86},[80,433,222],{"style":96},[80,435,225],{"style":86},[80,437,438],{"style":92}," DefaultValuePipe",[80,440,128],{"style":96},[80,442,443],{"style":258},"\"development\"",[80,445,62],{"style":86},[80,447,264],{"style":92},[80,449,450],{"style":96},"());",[80,452,453,455,458,460,462,464,466,468,471,473,475,477,479,481],{"class":82,"style":83},[80,454,219],{"style":86},[80,456,457],{"style":96}," env ",[80,459,225],{"style":86},[80,461,248],{"style":96},[80,463,62],{"style":86},[80,465,253],{"style":92},[80,467,128],{"style":96},[80,469,470],{"style":258},"\"\"",[80,472,62],{"style":86},[80,474,264],{"style":92},[80,476,267],{"style":96},[80,478,270],{"style":86},[80,480,273],{"style":96},[80,482,483],{"style":276},"\u002F\u002F \"development\"",[80,485,486,488,490,492,494,496,498,500,503,505,507,509,511,514],{"class":82,"style":83},[80,487,219],{"style":86},[80,489,457],{"style":96},[80,491,225],{"style":86},[80,493,248],{"style":96},[80,495,62],{"style":86},[80,497,253],{"style":92},[80,499,128],{"style":96},[80,501,502],{"style":258},"\"prod\"",[80,504,62],{"style":86},[80,506,264],{"style":92},[80,508,267],{"style":96},[80,510,270],{"style":86},[80,512,513],{"style":96},"; ",[80,515,516],{"style":276},"\u002F\u002F \"prod\"",[64,518,31],{"id":30},[51,520,521],{},"Implement the trait on any value:",[72,523,524],{"language":74,"class":75},[60,525,526,104,544,104,546,104,558,104,560,104,582,104,595,104,597,104,635,104,659,104,683,104,688,104,726,104,731],{"class":78},[80,527,528,530,532,534,536,538,540,542],{"class":82,"style":83},[80,529,203],{"style":86},[80,531,206],{"style":92},[80,533,161],{"style":86},[80,535,322],{"style":96},[80,537,69],{"style":92},[80,539,167],{"style":96},[80,541,35],{"style":92},[80,543,336],{"style":96},[80,545],{"class":82,"style":83},[80,547,548,550,553,556],{"class":82,"style":83},[80,549,87],{"style":86},[80,551,552],{"style":86}," struct",[80,554,555],{"style":92}," SlugPipe",[80,557,115],{"style":96},[80,559],{"class":82,"style":83},[80,561,562,565,567,569,571,574,577,579],{"class":82,"style":83},[80,563,564],{"style":86},"impl",[80,566,93],{"style":92},[80,568,97],{"style":96},[80,570,190],{"style":92},[80,572,573],{"style":96},"> ",[80,575,576],{"style":86},"for",[80,578,555],{"style":92},[80,580,581],{"style":96}," {",[80,583,584,586,588,590,593],{"class":82,"style":83},[80,585,109],{"style":86},[80,587,112],{"style":92},[80,589,245],{"style":86},[80,591,592],{"style":92}," String",[80,594,115],{"style":96},[80,596],{"class":82,"style":83},[80,598,599,601,603,605,607,609,611,613,615,617,619,621,623,625,627,629,631,633],{"class":82,"style":83},[80,600,122],{"style":86},[80,602,125],{"style":92},[80,604,128],{"style":96},[80,606,131],{"style":86},[80,608,135],{"style":134},[80,610,138],{"style":96},[80,612,141],{"style":86},[80,614,592],{"style":92},[80,616,147],{"style":96},[80,618,150],{"style":86},[80,620,153],{"style":92},[80,622,97],{"style":96},[80,624,158],{"style":134},[80,626,161],{"style":86},[80,628,164],{"style":92},[80,630,167],{"style":96},[80,632,35],{"style":92},[80,634,103],{"style":96},[80,636,637,640,643,645,648,651,653,656],{"class":82,"style":83},[80,638,639],{"style":86},"        if",[80,641,642],{"style":96}," value",[80,644,62],{"style":86},[80,646,647],{"style":92},"trim",[80,649,650],{"style":96},"()",[80,652,62],{"style":86},[80,654,655],{"style":92},"is_empty",[80,657,658],{"style":96},"() {",[80,660,661,664,667,669,671,673,675,677,680],{"class":82,"style":83},[80,662,663],{"style":86},"            return",[80,665,666],{"style":92}," Err",[80,668,128],{"style":96},[80,670,35],{"style":92},[80,672,161],{"style":86},[80,674,384],{"style":92},[80,676,128],{"style":96},[80,678,679],{"style":258},"\"slug must not be empty\"",[80,681,682],{"style":96},"));",[80,684,685],{"class":82,"style":83},[80,686,687],{"style":96},"        }",[80,689,690,693,696,698,700,702,704,707,709,711,714,716,719,721,724],{"class":82,"style":83},[80,691,692],{"style":92},"        Ok",[80,694,695],{"style":96},"(value",[80,697,62],{"style":86},[80,699,647],{"style":92},[80,701,650],{"style":96},[80,703,62],{"style":86},[80,705,706],{"style":92},"to_lowercase",[80,708,650],{"style":96},[80,710,62],{"style":86},[80,712,713],{"style":92},"replace",[80,715,128],{"style":96},[80,717,718],{"style":258},"' '",[80,720,167],{"style":96},[80,722,723],{"style":258},"\"-\"",[80,725,392],{"style":96},[80,727,728],{"class":82,"style":83},[80,729,730],{"style":96},"    }",[80,732,733],{"class":82,"style":83},[80,734,177],{"style":96},[181,736,737],{"id":34},[60,738,35],{},[72,740,741],{"language":74,"class":75},[60,742,743,104,768,104,789,104,816,104,837],{"class":78},[80,744,745,747,750,752,755,757,760,762,765],{"class":82,"style":83},[80,746,219],{"style":86},[80,748,749],{"style":96}," err ",[80,751,225],{"style":86},[80,753,754],{"style":92}," PipeError",[80,756,161],{"style":86},[80,758,759],{"style":92},"new",[80,761,128],{"style":96},[80,763,764],{"style":258},"\"message\"",[80,766,767],{"style":96},");",[80,769,770,772,774,776,778,780,783,786],{"class":82,"style":83},[80,771,219],{"style":86},[80,773,749],{"style":96},[80,775,225],{"style":86},[80,777,754],{"style":92},[80,779,161],{"style":86},[80,781,782],{"style":92},"response",[80,784,785],{"style":96},"(response);  ",[80,787,788],{"style":276},"\u002F\u002F wrap an arbitrary Response",[80,790,791,793,795,797,799,801,803,805,808,811,813],{"class":82,"style":83},[80,792,219],{"style":86},[80,794,749],{"style":96},[80,796,225],{"style":86},[80,798,754],{"style":92},[80,800,161],{"style":86},[80,802,384],{"style":92},[80,804,128],{"style":96},[80,806,807],{"style":258},"\"...\"",[80,809,810],{"style":96},")",[80,812,141],{"style":86},[80,814,815],{"style":276},"  \u002F\u002F shorthand constructors",[80,817,818,820,822,824,826,828,831,833,835],{"class":82,"style":83},[80,819,219],{"style":86},[80,821,749],{"style":96},[80,823,225],{"style":86},[80,825,754],{"style":92},[80,827,161],{"style":86},[80,829,830],{"style":92},"forbidden",[80,832,128],{"style":96},[80,834,807],{"style":258},[80,836,767],{"style":96},[80,838,839,841,843,845,847,849,852,854,856],{"class":82,"style":83},[80,840,219],{"style":86},[80,842,749],{"style":96},[80,844,225],{"style":86},[80,846,754],{"style":92},[80,848,161],{"style":86},[80,850,851],{"style":92},"not_found",[80,853,128],{"style":96},[80,855,807],{"style":258},[80,857,767],{"style":96},[51,859,860,863,864,867],{},[60,861,862],{},"PipeError::bad_request"," renders as a 400; ",[60,865,866],{},"PipeError::response"," lets you short-circuit with a fully-formed response.",[64,869,38],{"id":37},[51,871,872,873,876,877,882,883,886,887,890,891,894,895,898],{},"The validation crate ships ",[60,874,875],{},"ValidationPipe\u003CRhs?>"," (",[878,879,881],"a",{"href":880},"\u002Fdocs\u002Foverview\u002Fvalidation","source","): it runs ",[60,884,885],{},"Validate"," on the value and maps errors to a ",[60,888,889],{},"PipeError::response(422)"," carrying ",[60,892,893],{},"ValidationErrors",". It integrates with the validating ",[60,896,897],{},"ferrite_framework::Json\u003CT>"," extractor, which validates automatically on deserialization.",[64,900,41],{"id":40},[72,902,904],{"language":903,"class":75},"text",[60,905,907],{"class":906},"language-text",[80,908,909],{"class":82,"style":83},[80,910,911],{},"Middleware → Guards → Interceptors(before) → Pipes → Handler",[51,913,914],{},"A pipe failure aborts the pipeline and (if applicable) passes through registered exception filters.",[64,916,44],{"id":43},[918,919,920,926],"ul",{},[921,922,923,62],"li",{},[878,924,925],{"href":880},"Validation",[921,927,928,62],{},[878,929,931],{"href":930},"\u002Fdocs\u002Foverview\u002Fexception-filters","Exception filters",{"title":12,"searchDepth":13,"depth":14,"links":933},[934,935,939,942,943,944],{"id":17,"depth":13,"text":18},{"id":20,"depth":13,"text":21,"children":936},[937,938],{"id":24,"depth":14,"text":25},{"id":27,"depth":14,"text":28},{"id":30,"depth":13,"text":31,"children":940},[941],{"id":34,"depth":14,"text":35},{"id":37,"depth":13,"text":38},{"id":40,"depth":13,"text":41},{"id":43,"depth":13,"text":44},"https:\u002F\u002Fgithub.com\u002Fj4flmao\u002Fferrite_rs\u002Fedit\u002Fmain\u002Fdocs\u002Fcontent\u002Foverview\u002Fpipes.md",{"title":947,"slug":948},"Interceptors","overview\u002Finterceptors",{"title":931,"slug":950},"overview\u002Fexception-filters",1789565440437]