[{"data":1,"prerenderedAt":1060},["ShallowReactive",2],{"$f-k0RukBYq5pDd7Ui48a24pUKLTaZVGZ0OMmCn5hMKy8":3},{"slug":4,"title":5,"description":6,"source":7,"document":8,"toc":1042,"editUrl":1053,"prev":1054,"next":1057},"overview\u002Fvalidation","Validation","Validate DTOs at the boundary with","---\ntitle: Validation\ndescription: Validate DTOs at the boundary with #[derive(Validate)], the validating Json\u003CT> extractor and ValidationPipe.\n---\n\n# Validation\n\nFerrite validates data **at the boundary** — when it crosses into your application. Define declarative rules on your DTOs with `#[derive(Validate)]`, then let the validating `Json\u003CT>` extractor and `ValidationPipe` reject bad payloads.\n\n## `#[derive(Validate)]`\n\n```rust\nuse serde::Deserialize;\nuse ferrite_framework::{Validate, Json};\n\n#[derive(Debug, Deserialize, Validate)]\npub struct CreateUserDto {\n    #[validate(email)]\n    pub email: String,\n\n    #[validate(not_empty)]\n    #[validate(length(min = 3, max = 64))]\n    pub name: String,\n\n    #[validate(range(min = 18, max = 120))]\n    pub age: i32,\n}\n```\n\n### Rules\n\n| Rule | Meaning |\n| --- | --- |\n| `#[validate(email)]` | Value must look like an email |\n| `#[validate(not_empty)]` | Non-empty string |\n| `#[validate(length(min = n, max = m))]` | Length bounds |\n| `#[validate(range(min = n, max = m))]` | Numeric bounds |\n\n> Fields that are `Option` or `Vec` and generic structs are rejected at compile time.\n\n## The validating `Json\u003CT>` extractor\n\nReturn it from a handler and validation happens on deserialize:\n\n```rust\nuse ferrite_framework::{controller, impl_controller, post, Json, HttpError};\nuse ferrite_framework::{Validate};\nuse serde::Deserialize;\n\n#[derive(Deserialize, Validate)]\npub struct CreateUserDto { \u002F* ... *\u002F }\n\n#[impl_controller]\nimpl UsersController {\n    #[post(\"\u002F\")]\n    pub async fn create(&self, Json(dto): Json\u003CCreateUserDto>) -> Result\u003CJson\u003CUser>, HttpError> {\n        \u002F\u002F dto is already validated here\n        Ok(Json(self.service.create(&dto).await?))\n    }\n}\n```\n\nBehavior of `ferrite_framework::Json\u003CT>` (which is *not* the plain Axum `Json`):\n\n| Condition | Response |\n| --- | --- |\n| Malformed JSON | `400` `{\"statusCode\":400,\"message\":\"invalid JSON body\"}` |\n| Validation fails | `422` with the full errors object |\n| Valid | `200` with the validated value |\n\n## The errors object\n\n`ValidationErrors` serializes per-field:\n\n```json\n{\n  \"errors\": {\n    \"email\": [\"email must be a valid email address\"],\n    \"name\": [\"name must not be empty\", \"name must be between 3 and 64 characters\"]\n  }\n}\n```\n\n`FieldError { field, message }` is the atom:\n\n```rust\npub struct FieldError { pub field: String, pub message: String }\n```\n\n## Manual validation\n\nValidate anything that implements `Validate`:\n\n```rust\nuse ferrite_framework::{Validate, ValidationErrors};\n\nlet dto: CreateUserDto = serde_json::from_str(&body)?;\nif let Err(errors) = dto.validate() {\n    \u002F\u002F errors: ValidationErrors\n}\n```\n\n`ValidationErrors` exposes:\n\n- `new()`, `is_empty()`, `push()`\n- `errors() -> &[FieldError]`\n- `to_value()` → the JSON shape above\n- `IntoResponse` → HTTP **422**\n\n## `ValidationPipe`\n\nFor inputs that aren't a raw JSON body, use the pipe:\n\n```rust\nuse ferrite_framework::{Pipe, ValidationPipe};\n\nlet pipe = ValidationPipe;\nlet validated = pipe.transform(raw)?;   \u002F\u002F Err(PipeError::response(422)) on failure\n```\n\nIt maps `Validate` failures to a `PipeError::response(422)` carrying `ValidationErrors`.\n\n## Difference from NestJS `class-validator`\n\n| | NestJS | Ferrite |\n| --- | --- | --- |\n| Rules | annotations + decorators | attribute macros + derive |\n| Runtime | class-transformer instantiation | compile-time validation impl |\n| Location | `ValidationPipe` global by default | extractor + explicit pipe |\n\n## Next steps\n\n- [Pipes](\u002Fdocs\u002Foverview\u002Fpipes).\n- [Controllers](\u002Fdocs\u002Foverview\u002Fcontrollers).",{"frontmatter":9,"meta":10,"nodes":41},{"title":5,"description":6},{"toc":11},{"title":12,"searchDepth":13,"depth":14,"links":15},"On This Page",2,3,[16,23,26,29,32,35,38],{"id":17,"depth":13,"text":18,"children":19},"derivevalidate","#[derive(Validate)]",[20],{"id":21,"depth":14,"text":22},"derivevalidate-rules","Rules",{"id":24,"depth":13,"text":25},"the-validating-jsont-extractor","The validating Json\u003CT> extractor",{"id":27,"depth":13,"text":28},"the-errors-object","The errors object",{"id":30,"depth":13,"text":31},"manual-validation","Manual validation",{"id":33,"depth":13,"text":34},"validationpipe","ValidationPipe",{"id":36,"depth":13,"text":37},"difference-from-nestjs-class-validator","Difference from NestJS class-validator",{"id":39,"depth":13,"text":40},"next-steps","Next steps",[42,46,66,71,256,259,319,331,337,340,568,583,633,635,641,700,706,743,745,750,844,849,883,887,890,957,969,975,1025,1027],[43,44,5],"h1",{"id":45},"validation",[47,48,49,50,54,55,58,59,62,63,65],"p",{},"Ferrite validates data ",[51,52,53],"strong",{},"at the boundary"," — when it crosses into your application. Define declarative rules on your DTOs with ",[56,57,18],"code",{},", then let the validating ",[56,60,61],{},"Json\u003CT>"," extractor and ",[56,64,34],{}," reject bad payloads.",[67,68,69],"h2",{"id":17},[56,70,18],{},[72,73,76],"pre",{"language":74,"class":75},"rust","shiki shiki-themes github-light github-dark dark:github-dark",[56,77,79,102,103,102,127,102,129,102,148,102,162,102,167,102,184,102,186,102,191,102,207,102,220,102,222,102,237,102,251],{"class":78},"language-rust",[80,81,84,88,92,95,98],"span",{"class":82,"style":83},"line","display: inline",[80,85,87],{"style":86},"color:#D73A49;--shiki-dark:#F97583","use",[80,89,91],{"style":90},"color:#6F42C1;--shiki-dark:#B392F0"," serde",[80,93,94],{"style":86},"::",[80,96,97],{"style":90},"Deserialize",[80,99,101],{"style":100},"color:#24292E;--shiki-dark:#E1E4E8",";","\n",[80,104,105,107,110,112,115,118,121,124],{"class":82,"style":83},[80,106,87],{"style":86},[80,108,109],{"style":90}," ferrite_framework",[80,111,94],{"style":86},[80,113,114],{"style":100},"{",[80,116,117],{"style":90},"Validate",[80,119,120],{"style":100},", ",[80,122,123],{"style":90},"Json",[80,125,126],{"style":100},"};",[80,128],{"class":82,"style":83},[80,130,131,134,137,139,141,143,145],{"class":82,"style":83},[80,132,133],{"style":100},"#[derive(",[80,135,136],{"style":90},"Debug",[80,138,120],{"style":100},[80,140,97],{"style":90},[80,142,120],{"style":100},[80,144,117],{"style":90},[80,146,147],{"style":100},")]",[80,149,150,153,156,159],{"class":82,"style":83},[80,151,152],{"style":86},"pub",[80,154,155],{"style":86}," struct",[80,157,158],{"style":90}," CreateUserDto",[80,160,161],{"style":100}," {",[80,163,164],{"class":82,"style":83},[80,165,166],{"style":100},"    #[validate(email)]",[80,168,169,172,175,178,181],{"class":82,"style":83},[80,170,171],{"style":86},"    pub",[80,173,174],{"style":100}," email",[80,176,177],{"style":86},":",[80,179,180],{"style":90}," String",[80,182,183],{"style":100},",",[80,185],{"class":82,"style":83},[80,187,188],{"class":82,"style":83},[80,189,190],{"style":100},"    #[validate(not_empty)]",[80,192,193,196,199,202,204],{"class":82,"style":83},[80,194,195],{"style":100},"    #[validate(length(min ",[80,197,198],{"style":86},"=",[80,200,201],{"style":100}," 3, max ",[80,203,198],{"style":86},[80,205,206],{"style":100}," 64))]",[80,208,209,211,214,216,218],{"class":82,"style":83},[80,210,171],{"style":86},[80,212,213],{"style":100}," name",[80,215,177],{"style":86},[80,217,180],{"style":90},[80,219,183],{"style":100},[80,221],{"class":82,"style":83},[80,223,224,227,229,232,234],{"class":82,"style":83},[80,225,226],{"style":100},"    #[validate(range(min ",[80,228,198],{"style":86},[80,230,231],{"style":100}," 18, max ",[80,233,198],{"style":86},[80,235,236],{"style":100}," 120))]",[80,238,239,241,244,246,249],{"class":82,"style":83},[80,240,171],{"style":86},[80,242,243],{"style":100}," age",[80,245,177],{"style":86},[80,247,248],{"style":90}," i32",[80,250,183],{"style":100},[80,252,253],{"class":82,"style":83},[80,254,255],{"style":100},"}",[257,258,22],"h3",{"id":21},[260,261,262,275],"table",{},[263,264,265],"thead",{},[266,267,268,272],"tr",{},[269,270,271],"th",{},"Rule",[269,273,274],{},"Meaning",[276,277,278,289,299,309],"tbody",{},[266,279,280,286],{},[281,282,283],"td",{},[56,284,285],{},"#[validate(email)]",[281,287,288],{},"Value must look like an email",[266,290,291,296],{},[281,292,293],{},[56,294,295],{},"#[validate(not_empty)]",[281,297,298],{},"Non-empty string",[266,300,301,306],{},[281,302,303],{},[56,304,305],{},"#[validate(length(min = n, max = m))]",[281,307,308],{},"Length bounds",[266,310,311,316],{},[281,312,313],{},[56,314,315],{},"#[validate(range(min = n, max = m))]",[281,317,318],{},"Numeric bounds",[320,321,322,323,326,327,330],"blockquote",{},"Fields that are ",[56,324,325],{},"Option"," or ",[56,328,329],{},"Vec"," and generic structs are rejected at compile time.",[67,332,333,334,336],{"id":24},"The validating ",[56,335,61],{}," extractor",[47,338,339],{},"Return it from a handler and validation happens on deserialize:",[72,341,342],{"language":74,"class":75},[56,343,344,102,364,102,378,102,390,102,392,102,404,102,422,102,424,102,429,102,439,102,450,102,517,102,522,102,559,102,564],{"class":78},[80,345,346,348,350,352,355,357,359,362],{"class":82,"style":83},[80,347,87],{"style":86},[80,349,109],{"style":90},[80,351,94],{"style":86},[80,353,354],{"style":100},"{controller, impl_controller, post, ",[80,356,123],{"style":90},[80,358,120],{"style":100},[80,360,361],{"style":90},"HttpError",[80,363,126],{"style":100},[80,365,366,368,370,372,374,376],{"class":82,"style":83},[80,367,87],{"style":86},[80,369,109],{"style":90},[80,371,94],{"style":86},[80,373,114],{"style":100},[80,375,117],{"style":90},[80,377,126],{"style":100},[80,379,380,382,384,386,388],{"class":82,"style":83},[80,381,87],{"style":86},[80,383,91],{"style":90},[80,385,94],{"style":86},[80,387,97],{"style":90},[80,389,101],{"style":100},[80,391],{"class":82,"style":83},[80,393,394,396,398,400,402],{"class":82,"style":83},[80,395,133],{"style":100},[80,397,97],{"style":90},[80,399,120],{"style":100},[80,401,117],{"style":90},[80,403,147],{"style":100},[80,405,406,408,410,412,415,419],{"class":82,"style":83},[80,407,152],{"style":86},[80,409,155],{"style":86},[80,411,158],{"style":90},[80,413,414],{"style":100}," { ",[80,416,418],{"style":417},"color:#6A737D;--shiki-dark:#6A737D","\u002F* ... *\u002F",[80,420,421],{"style":100}," }",[80,423],{"class":82,"style":83},[80,425,426],{"class":82,"style":83},[80,427,428],{"style":100},"#[impl_controller]",[80,430,431,434,437],{"class":82,"style":83},[80,432,433],{"style":86},"impl",[80,435,436],{"style":90}," UsersController",[80,438,161],{"style":100},[80,440,441,444,448],{"class":82,"style":83},[80,442,443],{"style":100},"    #[post(",[80,445,447],{"style":446},"color:#032F62;--shiki-dark:#9ECBFF","\"\u002F\"",[80,449,147],{"style":100},[80,451,452,454,457,460,463,466,469,473,475,477,480,482,485,488,491,494,497,500,502,504,506,509,512,514],{"class":82,"style":83},[80,453,171],{"style":86},[80,455,456],{"style":86}," async",[80,458,459],{"style":86}," fn",[80,461,462],{"style":90}," create",[80,464,465],{"style":100},"(",[80,467,468],{"style":86},"&",[80,470,472],{"style":471},"color:#005CC5;--shiki-dark:#79B8FF","self",[80,474,120],{"style":100},[80,476,123],{"style":90},[80,478,479],{"style":100},"(dto)",[80,481,177],{"style":86},[80,483,484],{"style":90}," Json",[80,486,487],{"style":100},"\u003C",[80,489,490],{"style":90},"CreateUserDto",[80,492,493],{"style":100},">) ",[80,495,496],{"style":86},"->",[80,498,499],{"style":90}," Result",[80,501,487],{"style":100},[80,503,123],{"style":90},[80,505,487],{"style":100},[80,507,508],{"style":90},"User",[80,510,511],{"style":100},">, ",[80,513,361],{"style":90},[80,515,516],{"style":100},"> {",[80,518,519],{"class":82,"style":83},[80,520,521],{"style":417},"        \u002F\u002F dto is already validated here",[80,523,524,527,529,531,533,535,538,541,543,546,548,550,553,556],{"class":82,"style":83},[80,525,526],{"style":90},"        Ok",[80,528,465],{"style":100},[80,530,123],{"style":90},[80,532,465],{"style":100},[80,534,472],{"style":471},[80,536,537],{"style":86},".",[80,539,540],{"style":100},"service",[80,542,537],{"style":86},[80,544,545],{"style":90},"create",[80,547,465],{"style":100},[80,549,468],{"style":86},[80,551,552],{"style":100},"dto)",[80,554,555],{"style":86},".await?",[80,557,558],{"style":100},"))",[80,560,561],{"class":82,"style":83},[80,562,563],{"style":100},"    }",[80,565,566],{"class":82,"style":83},[80,567,255],{"style":100},[47,569,570,571,574,575,579,580,582],{},"Behavior of ",[56,572,573],{},"ferrite_framework::Json\u003CT>"," (which is ",[576,577,578],"em",{},"not"," the plain Axum ",[56,581,123],{},"):",[260,584,585,595],{},[263,586,587],{},[266,588,589,592],{},[269,590,591],{},"Condition",[269,593,594],{},"Response",[276,596,597,611,622],{},[266,598,599,602],{},[281,600,601],{},"Malformed JSON",[281,603,604,607,608],{},[56,605,606],{},"400"," ",[56,609,610],{},"{\"statusCode\":400,\"message\":\"invalid JSON body\"}",[266,612,613,616],{},[281,614,615],{},"Validation fails",[281,617,618,621],{},[56,619,620],{},"422"," with the full errors object",[266,623,624,627],{},[281,625,626],{},"Valid",[281,628,629,632],{},[56,630,631],{},"200"," with the validated value",[67,634,28],{"id":27},[47,636,637,640],{},[56,638,639],{},"ValidationErrors"," serializes per-field:",[72,642,644],{"language":643,"class":75},"json",[56,645,647,102,651,102,659,102,673,102,691,102,696],{"class":646},"language-json",[80,648,649],{"class":82,"style":83},[80,650,114],{"style":100},[80,652,653,656],{"class":82,"style":83},[80,654,655],{"style":471},"  \"errors\"",[80,657,658],{"style":100},": {",[80,660,661,664,667,670],{"class":82,"style":83},[80,662,663],{"style":471},"    \"email\"",[80,665,666],{"style":100},": [",[80,668,669],{"style":446},"\"email must be a valid email address\"",[80,671,672],{"style":100},"],",[80,674,675,678,680,683,685,688],{"class":82,"style":83},[80,676,677],{"style":471},"    \"name\"",[80,679,666],{"style":100},[80,681,682],{"style":446},"\"name must not be empty\"",[80,684,120],{"style":100},[80,686,687],{"style":446},"\"name must be between 3 and 64 characters\"",[80,689,690],{"style":100},"]",[80,692,693],{"class":82,"style":83},[80,694,695],{"style":100},"  }",[80,697,698],{"class":82,"style":83},[80,699,255],{"style":100},[47,701,702,705],{},[56,703,704],{},"FieldError { field, message }"," is the atom:",[72,707,708],{"language":74,"class":75},[56,709,710],{"class":78},[80,711,712,714,716,719,721,723,726,728,730,732,734,737,739,741],{"class":82,"style":83},[80,713,152],{"style":86},[80,715,155],{"style":86},[80,717,718],{"style":90}," FieldError",[80,720,414],{"style":100},[80,722,152],{"style":86},[80,724,725],{"style":100}," field",[80,727,177],{"style":86},[80,729,180],{"style":90},[80,731,120],{"style":100},[80,733,152],{"style":86},[80,735,736],{"style":100}," message",[80,738,177],{"style":86},[80,740,180],{"style":90},[80,742,421],{"style":100},[67,744,31],{"id":30},[47,746,747,748,177],{},"Validate anything that implements ",[56,749,117],{},[72,751,752],{"language":74,"class":75},[56,753,754,102,772,102,774,102,809,102,835,102,840],{"class":78},[80,755,756,758,760,762,764,766,768,770],{"class":82,"style":83},[80,757,87],{"style":86},[80,759,109],{"style":90},[80,761,94],{"style":86},[80,763,114],{"style":100},[80,765,117],{"style":90},[80,767,120],{"style":100},[80,769,639],{"style":90},[80,771,126],{"style":100},[80,773],{"class":82,"style":83},[80,775,776,779,782,784,786,789,792,794,797,799,801,804,807],{"class":82,"style":83},[80,777,778],{"style":86},"let",[80,780,781],{"style":100}," dto",[80,783,177],{"style":86},[80,785,158],{"style":90},[80,787,788],{"style":86}," =",[80,790,791],{"style":90}," serde_json",[80,793,94],{"style":86},[80,795,796],{"style":90},"from_str",[80,798,465],{"style":100},[80,800,468],{"style":86},[80,802,803],{"style":100},"body)",[80,805,806],{"style":86},"?",[80,808,101],{"style":100},[80,810,811,814,817,820,823,825,827,829,832],{"class":82,"style":83},[80,812,813],{"style":86},"if",[80,815,816],{"style":86}," let",[80,818,819],{"style":90}," Err",[80,821,822],{"style":100},"(errors) ",[80,824,198],{"style":86},[80,826,781],{"style":100},[80,828,537],{"style":86},[80,830,831],{"style":90},"validate",[80,833,834],{"style":100},"() {",[80,836,837],{"class":82,"style":83},[80,838,839],{"style":417},"    \u002F\u002F errors: ValidationErrors",[80,841,842],{"class":82,"style":83},[80,843,255],{"style":100},[47,845,846,848],{},[56,847,639],{}," exposes:",[850,851,852,864,869,875],"ul",{},[853,854,855,120,858,120,861],"li",{},[56,856,857],{},"new()",[56,859,860],{},"is_empty()",[56,862,863],{},"push()",[853,865,866],{},[56,867,868],{},"errors() -> &[FieldError]",[853,870,871,874],{},[56,872,873],{},"to_value()"," → the JSON shape above",[853,876,877,880,881],{},[56,878,879],{},"IntoResponse"," → HTTP ",[51,882,620],{},[67,884,885],{"id":33},[56,886,34],{},[47,888,889],{},"For inputs that aren't a raw JSON body, use the pipe:",[72,891,892],{"language":74,"class":75},[56,893,894,102,913,102,915,102,929],{"class":78},[80,895,896,898,900,902,904,907,909,911],{"class":82,"style":83},[80,897,87],{"style":86},[80,899,109],{"style":90},[80,901,94],{"style":86},[80,903,114],{"style":100},[80,905,906],{"style":90},"Pipe",[80,908,120],{"style":100},[80,910,34],{"style":90},[80,912,126],{"style":100},[80,914],{"class":82,"style":83},[80,916,917,919,922,924,927],{"class":82,"style":83},[80,918,778],{"style":86},[80,920,921],{"style":100}," pipe ",[80,923,198],{"style":86},[80,925,926],{"style":90}," ValidationPipe",[80,928,101],{"style":100},[80,930,931,933,936,938,941,943,946,949,951,954],{"class":82,"style":83},[80,932,778],{"style":86},[80,934,935],{"style":100}," validated ",[80,937,198],{"style":86},[80,939,940],{"style":100}," pipe",[80,942,537],{"style":86},[80,944,945],{"style":90},"transform",[80,947,948],{"style":100},"(raw)",[80,950,806],{"style":86},[80,952,953],{"style":100},";   ",[80,955,956],{"style":417},"\u002F\u002F Err(PipeError::response(422)) on failure",[47,958,959,960,962,963,966,967,537],{},"It maps ",[56,961,117],{}," failures to a ",[56,964,965],{},"PipeError::response(422)"," carrying ",[56,968,639],{},[67,970,971,972],{"id":36},"Difference from NestJS ",[56,973,974],{},"class-validator",[260,976,977,989],{},[263,978,979],{},[266,980,981,983,986],{},[269,982],{},[269,984,985],{},"NestJS",[269,987,988],{},"Ferrite",[276,990,991,1001,1012],{},[266,992,993,995,998],{},[281,994,22],{},[281,996,997],{},"annotations + decorators",[281,999,1000],{},"attribute macros + derive",[266,1002,1003,1006,1009],{},[281,1004,1005],{},"Runtime",[281,1007,1008],{},"class-transformer instantiation",[281,1010,1011],{},"compile-time validation impl",[266,1013,1014,1017,1022],{},[281,1015,1016],{},"Location",[281,1018,1019,1021],{},[56,1020,34],{}," global by default",[281,1023,1024],{},"extractor + explicit pipe",[67,1026,40],{"id":39},[850,1028,1029,1036],{},[853,1030,1031,537],{},[1032,1033,1035],"a",{"href":1034},"\u002Fdocs\u002Foverview\u002Fpipes","Pipes",[853,1037,1038,537],{},[1032,1039,1041],{"href":1040},"\u002Fdocs\u002Foverview\u002Fcontrollers","Controllers",{"title":12,"searchDepth":13,"depth":14,"links":1043},[1044,1047,1048,1049,1050,1051,1052],{"id":17,"depth":13,"text":18,"children":1045},[1046],{"id":21,"depth":14,"text":22},{"id":24,"depth":13,"text":25},{"id":27,"depth":13,"text":28},{"id":30,"depth":13,"text":31},{"id":33,"depth":13,"text":34},{"id":36,"depth":13,"text":37},{"id":39,"depth":13,"text":40},"https:\u002F\u002Fgithub.com\u002Fj4flmao\u002Fferrite_rs\u002Fedit\u002Fmain\u002Fdocs\u002Fcontent\u002Foverview\u002Fvalidation.md",{"title":1055,"slug":1056},"Configuration","overview\u002Fconfiguration",{"title":1058,"slug":1059},"CLI usage","overview\u002Fcli-usage",1789565440269]