1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
//! tide-validator is a middleware working with [Tide](https://github.com/http-rs/tide), a web framework in Rust //! which let you validate your data coming from a request. You'll be able //! to create custom validators to validate your HTTP parameters, query parameters, //! cookies and headers. //! //! # Features //! //! - __Custom validators:__ you can chain multiple validators and develop a custom validator is very easy. It's just a closure. //! - __Validate everything:__ with the enum `HttpField` you can validate different fields like cookies, headers, query parameters and parameters. //! - __Your own errors:__ thanks to generics in Rust you can use your own custom error when the data is invalid. //! need. //! //! # Validators //! //! To create your own validator it's just a closure to create with this form: //! //! ```rust,no_run,compile_fail //! // The first closure's parameter is the parameter/queryparameter/cookie/header name. //! // The second parameter is the value of this HTTP element. None means the field doesn't exist in the request (useful to force specific fields to be required). //! Fn(&str, Option<&str>) -> Result<(), T> + Send + Sync + 'static where T: Serialize + Send + Sync + 'static //! ``` //! //! # Examples //! //! __simple validation__ //! ```rust,no_run,compile_fail //! // Our own validator is a simple closure to check if the field is a number //! fn is_number(field_name: &str, field_value: Option<&str>) -> Result<(), String> { //! if let Some(field_value) = field_value { //! if field_value.parse::<i64>().is_err() { //! return Err(format!("field '{}' = '{}' is not a valid number", field_name, field_value)); //! } //! } //! //! Ok(()) //! } //! //! //... in main function //! let mut app = tide::new(); //! let mut validator_middleware = ValidatorMiddleware::new(); //! // 'age' is the parameter name inside the route '/test/:age' //! validator_middleware.add_validator(HttpField::Param("age"), is_number); //! // You can assign different middleware for each routes therefore different validators for each routes //! app.at("/test/:age") //! .middleware(validator_middleware) //! .get(|_: tide::Request<()>| async move { //! let cat = Cat { //! name: "Gribouille".into(), //! }; //! tide::Response::new(StatusCode::Ok).body_json(&cat).unwrap() //! }); //! app.listen("127.0.0.1:8080").await?; //! ``` //! //! __chain multiple validators__ //! ```rust,no_run,compile_fail //! // This validator force element to be required //! fn is_required(field_name: &str, field_value: Option<&str>) -> Result<(), String> { //! if field_value.is_none() { //! Err(format!("'{}' is required", field_name)) //! } else { //! Ok(()) //! } //! } //! //! // ... your main function //! //! let mut app = tide::new(); //! let mut validator_middleware = ValidatorMiddleware::new(); //! // Here 'age' is a query parameter, the validator stay the same as in previous example //! validator_middleware.add_validator(HttpField::QueryParam("age"), is_number); //! // You can also add multiple validators on a single query parameter to check different things //! validator_middleware.add_validator(HttpField::QueryParam("age"), is_required); //! //! // You can assign different middleware for each routes therefore different validators for each routes //! app.at("/test") //! .middleware(validator_middleware) //! .get(|_: tide::Request<()>| async move { //! let cat = Cat { //! name: "Mozart".into(), //! }; //! tide::Response::new(StatusCode::Ok).body_json(&cat).unwrap() //! }, //! ); //! //! app.listen("127.0.0.1:8080").await?; //! ``` //! //! __Use your own custom error__ //! ```rust,no_run,compile_fail //! // Your custom error which your api will send if an error occurs //! #[derive(Debug, Serialize)] //! struct CustomError { //! status_code: usize, //! message: String, //! } //! //! // Your validator can also return your own error type //! fn is_number(field_name: &str, field_value: Option<&str>) -> Result<(), CustomError> { //! if let Some(field_value) = field_value { //! if field_value.parse::<i64>().is_err() { //! return Err(CustomError { //! status_code: 400, //! message: format!( //! "field '{}' = '{}' is not a valid number", //! field_name, field_value //! ), //! }); //! } //! } //! Ok(()) //! } //! //! // ... your main function //! ``` //! //! __Dynamic validators__ //! ```rust,no_run,compile_fail //! // Validator inside a function as a closure to be dynamic with max_length //! fn is_length_under( //! max_length: usize, //! ) -> Box<dyn Fn(&str, Option<&str>) -> Result<(), CustomError> + Send + Sync + 'static> { //! Box::new( //! move |field_name: &str, field_value: Option<&str>| -> Result<(), CustomError> { //! if let Some(field_value) = field_value { //! if field_value.len() > max_length { //! let my_error = CustomError { //! status_code: 400, //! message: format!( //! "element '{} which is equals to '{}' have not the maximum length of {}", //! field_name, field_value, max_length //! ), //! }; //! return Err(my_error); //! } //! } //! Ok(()) //! }, //! ) //! } //! //! // Simply call it on a cookie `session` for example: //! validator_middleware.add_validator(HttpField::Cookie("session"), is_length_under(20)); //! //! ``` //! //! For more details about examples check out [the `examples` directory on GitHub](https://github.com/bnjjj/tide-validator/tree/master/examples) use std::collections::HashMap; use std::str::FromStr; use std::sync::Arc; use futures::future::BoxFuture; use serde::Serialize; use tide::{ http_types::{headers::HeaderName, StatusCode}, Middleware, Next, Request, Response, }; // trait Validator = Fn(&str) -> Result<(), String> + Send + Sync + 'static; /// Enum to indicate on which HTTP field you want to make validations #[derive(Debug, Clone, Hash, Eq, PartialEq)] pub enum HttpField<'a> { /// To validate a path parameter. Example in URL `/test/:name` you can use `HttpField::Param("name")` Param(&'a str), /// To validate a query parameter. Example in URL `/test?name=test` you can use `HttpField::QueryParam("name")` QueryParam(&'a str), /// To validate a header. Example `HttpField::Header("X-My-Custom-Header")` Header(&'a str), /// To validate a cookie. Example `HttpField::Cookie("session")` Cookie(&'a str), } /// Used as a middleware in your tide framework and add your custom validators pub struct ValidatorMiddleware<T> where T: Serialize + Send + Sync + 'static, { validators: HashMap< HttpField<'static>, Vec<Arc<dyn Fn(&str, Option<&str>) -> Result<(), T> + Send + Sync + 'static>>, >, } impl<T> ValidatorMiddleware<T> where T: Serialize + Send + Sync + 'static, { /// Create a new ValidatorMiddleware to put in your tide configuration. /// /// # Example /// /// ```rust,no_run,compile_fail /// fn main() -> io::Result<()> { /// task::block_on(async { /// let mut app = tide::new(); /// /// let mut validator_middleware = ValidatorMiddleware::new(); /// validator_middleware.add_validator(HttpField::Header("X-Custom-Header"), is_number); /// /// app.at("/test/:n").middleware(validator_middleware).get( /// |_: tide::Request<()>| async move { tide::Response::new(StatusCode::Ok).body_json("test").unwrap() }, /// ); /// /// app.listen("127.0.0.1:8080").await?; /// Ok(()) /// }) /// } /// ``` pub fn new() -> Self { ValidatorMiddleware { validators: HashMap::new(), } } pub fn with_validators<F>(mut self, validators: HashMap<HttpField<'static>, F>) -> Self where F: Fn(&str, Option<&str>) -> Result<(), T> + Send + Sync + 'static, { for (param_name, validator) in validators { self.add_validator(param_name, validator); } self } /// Add new validator for your middleware /// /// # Example /// /// ```rust,no_run,compile_fail /// fn main() -> io::Result<()> { /// task::block_on(async { /// let mut app = tide::new(); /// /// let mut validator_middleware = ValidatorMiddleware::new(); /// validator_middleware.add_validator(HttpField::Header("X-Custom-Header"), is_number); /// validator_middleware.add_validator(HttpField::QueryParam("myqueryparam"), is_required); /// /// app.at("/test/:n").middleware(validator_middleware).get( /// |_: tide::Request<()>| async move { tide::Response::new(StatusCode::Ok).body_json("test").unwrap() }, /// ); /// /// app.listen("127.0.0.1:8080").await?; /// Ok(()) /// }) /// } /// ``` pub fn add_validator<F>(&mut self, param_name: HttpField<'static>, validator: F) where F: Fn(&str, Option<&str>) -> Result<(), T> + Send + Sync + 'static, { let validator = Arc::new(validator); let validator_moved = Arc::clone(&validator); self.validators .entry(param_name.into()) .and_modify(|e| e.push(validator_moved)) .or_insert(vec![validator]); } } impl<State, T> Middleware<State> for ValidatorMiddleware<T> where State: Send + Sync + 'static, T: Serialize + Send + Sync + 'static, { fn handle<'a>(&'a self, ctx: Request<State>, next: Next<'a, State>) -> BoxFuture<'a, Response> { Box::pin(async move { let mut query_parameters: Option<HashMap<String, String>> = None; for (param_name, validators) in &self.validators { match param_name { HttpField::Param(param_name) => { for validator in validators { let param_found: Result<String, _> = ctx.param(param_name); if let Err(err) = validator(param_name, param_found.ok().as_ref().map(|p| &p[..])) { return Response::new(StatusCode::BadRequest).body_json(&err).unwrap_or_else( |err| { return Response::new(StatusCode::InternalServerError).body_string(format!( "cannot serialize your parameter validator for '{}' error : {:?}", param_name, err )); }, ); } } } HttpField::QueryParam(param_name) => { if query_parameters.is_none() { match ctx.query::<HashMap<String, String>>() { Err(err) => { return Response::new(StatusCode::InternalServerError) .body_string(format!( "cannot read query parameters: {:?}", err )) } Ok(qps) => query_parameters = Some(qps), } } let query_parameters = query_parameters.as_ref().unwrap(); for validator in validators { if let Err(err) = validator( param_name, query_parameters.get(¶m_name[..]).map(|p| &p[..]), ) { return Response::new(StatusCode::BadRequest).body_json(&err).unwrap_or_else( |err| { return Response::new(StatusCode::InternalServerError).body_string(format!( "cannot serialize your query parameter validator for '{}' error : {:?}", param_name, err )); }, ); } } } HttpField::Header(header_name) => { for validator in validators { let header_found: Option<&str> = ctx .header(&HeaderName::from_str(header_name).unwrap()) .map(|header| header.last().map(|val| val.as_str()).unwrap()); if let Err(err) = validator(header_name, header_found) { return Response::new(StatusCode::BadRequest).body_json(&err).unwrap_or_else( |err| { return Response::new(StatusCode::InternalServerError).body_string(format!( "cannot serialize your header validator for '{}' error : {:?}", header_name, err )); }, ); } } } HttpField::Cookie(cookie_name) => { for validator in validators { let cookie_found = ctx.cookie(cookie_name); if let Err(err) = validator(cookie_name, cookie_found.as_ref().map(|c| c.value())) { return Response::new(StatusCode::BadRequest).body_json(&err).unwrap_or_else( |err| { return Response::new(StatusCode::InternalServerError).body_string(format!( "cannot serialize your cookie validator for '{}' error : {:?}", cookie_name, err )); }, ); } } } } } next.run(ctx).await }) } } #[cfg(test)] mod tests { use super::{HttpField, StatusCode, ValidatorMiddleware}; use async_std::io::prelude::*; use futures::executor::block_on; use http_service_mock::make_server; use serde::{Deserialize, Serialize}; use tide::http_types; #[inline] fn is_number(field_name: &str, field_value: Option<&str>) -> Result<(), String> { if let Some(field_value) = field_value { if field_value.parse::<i64>().is_err() { return Err(format!( "field '{}' = '{}' is not a valid number", field_name, field_value )); } } Ok(()) } #[test] fn validator_simple() { let mut inner = tide::new(); let mut validators = ValidatorMiddleware::new(); validators.add_validator(HttpField::Param("bar"), is_number); inner .at("/foo/:bar") .middleware(validators) .get(|_| async { "foo" }); let mut server = make_server(inner.into_http_service()).unwrap(); let mut buf = Vec::new(); let req = http_types::Request::new( http_types::Method::Get, "http://localhost/foo/4".parse().unwrap(), ); let mut res = server.simulate(req).unwrap(); assert_eq!(res.status(), 200); block_on(res.read_to_end(&mut buf)).unwrap(); assert_eq!(&*buf, &*b"foo"); buf.clear(); let req = http_types::Request::new( http_types::Method::Get, "http://localhost/foo/bar".parse().unwrap(), ); let mut res = server.simulate(req).unwrap(); assert_eq!(res.status(), StatusCode::BadRequest); block_on(res.read_to_end(&mut buf)).unwrap(); assert_eq!( String::from_utf8_lossy(&buf[..]), String::from(r#""field 'bar' = 'bar' is not a valid number""#) ); } #[derive(Debug, Serialize, Deserialize)] struct CustomError { status_code: usize, message: String, } fn is_length_under( max_length: usize, ) -> Box<dyn Fn(&str, Option<&str>) -> Result<(), CustomError> + Send + Sync + 'static> { Box::new( move |field_name: &str, field_value: Option<&str>| -> Result<(), CustomError> { if let Some(field_value) = field_value { if field_value.len() > max_length { let my_error = CustomError { status_code: 400, message: format!( "element '{}' which is equals to '{}' have not the maximum length of {}", field_name, field_value, max_length ), }; return Err(my_error); } } Ok(()) }, ) } #[test] fn validator_custom() { let mut inner = tide::new(); let mut validators = ValidatorMiddleware::new(); validators.add_validator(HttpField::QueryParam("test"), is_length_under(10)); validators.add_validator(HttpField::Cookie("session"), is_length_under(10)); inner .at("/foo") .middleware(validators) .get(|_| async { "foo" }); let mut server = make_server(inner.into_http_service()).unwrap(); let mut buf = Vec::new(); let req = http_types::Request::new( http_types::Method::Get, "http://localhost/foo?test=coucou".parse().unwrap(), ); let mut res = server.simulate(req).unwrap(); assert_eq!(res.status(), 200); block_on(res.read_to_end(&mut buf)).unwrap(); assert_eq!(&*buf, &*b"foo"); buf.clear(); let req = http_types::Request::new( http_types::Method::Get, "http://localhost/foo?test=blablablablabla".parse().unwrap(), ); let mut res = server.simulate(req).unwrap(); assert_eq!(res.status(), StatusCode::BadRequest); block_on(res.read_to_end(&mut buf)).unwrap(); let err: CustomError = serde_json::from_slice(&buf[..]).unwrap(); assert_eq!(err.status_code, 400usize); assert_eq!( err.message, String::from("element 'test' which is equals to 'blablablablabla' have not the maximum length of 10") ); } #[inline] fn is_bool(field_name: &str, field_value: Option<&str>) -> Result<(), CustomError> { if let Some(field_value) = field_value { match field_value { "true" | "false" => return Ok(()), other => { return Err(CustomError { status_code: 400, message: format!( "field '{}' = '{}' is not a valid boolean", field_name, other ), }) } } } Ok(()) } #[inline] fn is_required(field_name: &str, field_value: Option<&str>) -> Result<(), CustomError> { if field_value.is_none() { Err(CustomError { status_code: 400, message: format!("'{}' is mandatory", field_name), }) } else { Ok(()) } } #[test] fn validator_chains() { let mut inner = tide::new(); let mut validators = ValidatorMiddleware::new(); validators.add_validator(HttpField::QueryParam("test"), is_length_under(10)); validators.add_validator(HttpField::Header("X-Is-Connected"), is_required); validators.add_validator(HttpField::Header("X-Is-Connected"), is_bool); inner .at("/foo") .middleware(validators) .get(|_| async { "foo" }); let mut server = make_server(inner.into_http_service()).unwrap(); let mut buf = Vec::new(); let mut req = http_types::Request::new( http_types::Method::Get, "http://localhost/foo?test=coucou".parse().unwrap(), ); req.insert_header("X-Is-Connected", "true").unwrap(); let mut res = server.simulate(req).unwrap(); assert_eq!(res.status(), 200); block_on(res.read_to_end(&mut buf)).unwrap(); assert_eq!(&*buf, &*b"foo"); buf.clear(); let req = http_types::Request::new( http_types::Method::Get, "http://localhost/foo?test=coucou".parse().unwrap(), ); let mut res = server.simulate(req).unwrap(); assert_eq!(res.status(), StatusCode::BadRequest); block_on(res.read_to_end(&mut buf)).unwrap(); let err: CustomError = serde_json::from_slice(&buf[..]).unwrap(); assert_eq!(err.status_code, 400usize); assert_eq!(err.message, String::from("'X-Is-Connected' is mandatory")); } }