You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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)]structPolygon{// if this were a FromForm, we could do `#[field(validate = range(3..)]`num_sides:u32,}#[post("/submit-polygon", data = "<polygon>")]fnsubmit(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)]structPolygon{#[validate(range = (3..))]num_sides:u32,}// would also work on forms:#[derive(FromForm,Validated)]structFooForm<'a>{#[validate(omits = "password")]password:&'a str,}
The text was updated successfully, but these errors were encountered:
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.
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 theDeserialize
d object, e.g.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 aValid<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:
The text was updated successfully, but these errors were encountered: