rocketjson

Crates.iorocketjson
lib.rsrocketjson
version
sourcesrc
created_at2021-08-31 20:57:39.579563
updated_at2024-11-02 14:26:51.573905
descriptionCrate for working with Json and Rocket
homepage
repositoryhttps://github.com/Nis5l/rocketjson
max_upload_size
id445298
Cargo.toml error:TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include`
size0
Nissl (Nis5l)

documentation

https://docs.rs/rocketjson

README

rocketjson

Current Crates.io Version

Crate for working with Json and Rocket.
Ultimately the goal is to have validated Structs enter and leave the endpoint as Json while having everything happen in the background.

Documentation

Documentation is on docs.rs

Example

#[macro_use] extern crate rocket;

#[derive(serde::Deserialize, validator::Validate, rocketjson::JsonBody)]
pub struct RegisterRequest {
   #[validate(length(min = 1))]
   username: String 
}

#[derive(serde::Serialize)]
pub struct RegisterResponse {
   message: String
}

#[post("/register", data="<data>")]
pub fn register(data: RegisterRequest) -> rocketjson::ApiResponse<RegisterResponse> {
   rocketjson::ApiResponse::new(rocket::http::Status::Ok, RegisterResponse { message: format!("Welcome {}", data.username) })
}

#[launch]
fn rocket() -> _ {
    rocket::build()
        .mount("/", routes![register]).
        register("/", vec![rocketjson::error::get_catcher()])
}
  • Input
{
    "username": "testuser"
}
  • Output 200 OK
{
    "message": "Welcome testuser"
}
  • Input
{
    "username": ""
}
  • Output 400 Bad Request
{
    "username": [
        {
            "code": "length",
            "message": null,
            "params": {
                "value": "",
                "min": 1
            }
        }
    ]
}

License

The license can be chosen to be either of the following:

Commit count: 23

cargo fmt