Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate non form-encoded body content #1915

Open
Flying-Toast opened this issue Sep 23, 2021 · 2 comments
Open

Validate non form-encoded body content #1915

Flying-Toast opened this issue Sep 23, 2021 · 2 comments
Labels
accepted An accepted request or suggestion request Request for new functionality

Comments

@Flying-Toast
Copy link
Contributor

Rocket has validation for Forms (rocket::form::validate), but when accepting other body content (e.g. JSON), there is no way to validate fields of the Deserialized object, e.g.

#[derive(Deserialize)]
struct Polygon {
    // if this were a FromForm, we could do `#[field(validate = range(3..)]`
    num_sides: u32,
}

#[post("/submit-polygon", data = "<polygon>")]
fn submit(polygon: Json<Polygon>) -> String {
    format!("polygon has {} sides", polygon.num_sides)
}

Polygons shouldn't be allowed to have less than 3 sides, but there is no simple way to enforce this. Ideally we could do something similar to form validation, with field attributes.

Why this feature can't or shouldn't live outside of Rocket

This could possibly be implemented outside of Rocket using some Validate derive and a Valid<T> guard that will only pass if validation succeeds, but that is less than ideal.

Ideal Solution

Ideally, the form-validation logic could be pulled out into its own independent trait, so that you can derive it on any type:

use rocket::Validated;

#[derive(Deserialize, Validated)]
struct Polygon {
    #[validate(range = (3..))]
    num_sides: u32,
}

// would also work on forms:
#[derive(FromForm, Validated)]
struct FooForm<'a> {
    #[validate(omits = "password")]
    password: &'a str,
}
@Flying-Toast Flying-Toast added the request Request for new functionality label Sep 23, 2021
@SergioBenitez
Copy link
Member

Polygons shouldn't be allowed to have less than 3 sides, but there is no simple way to enforce this. Ideally we could do something similar to form validation, with field attributes.

Yeah, there's nothing as simple as rocket's field attribute, but for posterity, the likely most straightforward approach would be to use serde's deserialize_with field attribute.

In general, I have some ideas about how to improve/unify data input/output that would include unifying validation across all data handled by a Rocket application. My hope is to write down a spec for the idea soon. Until then, consider this accepted.

@SergioBenitez SergioBenitez added the accepted An accepted request or suggestion label Sep 25, 2021
@IniterWorker
Copy link

Related #2110.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted An accepted request or suggestion request Request for new functionality
Projects
None yet
Development

No branches or pull requests

3 participants