What would be the preferred way to plug in something like valico to validate JSON body data against a schema before it is serialized into a struct that is returned to its route?
Would it be better to validate the struct after it has been passed to the route by rocket?
This is the sort of thing FromData was tailor-made for. To integrate valico, I would:
Create a new type. Let's call it ValidJSON
struct ValidJSON<T: Deserialize>(T);
Implement FromData for ValidJSON, likely by calling contrib::JSON::from_data in the implementation, doing the validation, and returning an appropriate Outcome depending on the results.
You'd then use the type in a route as follows:
#[post("/", data = "<val>")]
fn (val: ValidJSON<T>) ...
Ah, exactly what I was looking for. Thanks!
Most helpful comment
This is the sort of thing FromData was tailor-made for. To integrate valico, I would:
Create a new type. Let's call it ValidJSON.
Implement
FromDataforValidJSON, likely by calling contrib::JSON::from_data in the implementation, doing the validation, and returning an appropriateOutcomedepending on the results.You'd then use the type in a route as follows: