Validation in Rocket #2110
Replies: 3 comments 5 replies
-
Hi @somehowchris, I reimplemented the I am struggling too with the same issue. How to deal with a custom error message? rocket documentation says
I am guessing we need some changes from rocket core. Because we can't transform from the error handling, I am wondering if Do you have any progress on your side? B. r |
Beta Was this translation helpful? Give feedback.
-
Maybe a bit offtopic here, but there's also a problem with JSON syntax/data validation. The goals I have in the backend I'm currently developing are:
There are multiple problems that don't seem like they currently have a solution:
I wouldn't say that this issue is critical and I understand that serde's architecture values speed over user friendliness, but this feels like a problem that should be solved at some point since user friendly errors are something that's basically a standard in web APIs. This isn't also something that's going to be fundamentally solved on Rocket's level, but the solutions I've described could be at least considered (although I know that these requirements/problems might not apply to everyone and the best solution would be to write a JSON guard of my own). And maybe there's someone that found a better solutions than I have. |
Beta Was this translation helpful? Give feedback.
-
Any updates on this topic? |
Beta Was this translation helpful? Give feedback.
-
Hey :)
So, I've been using custom wrapping types like
struct UpperCase(pub String)
alongside some custom implementations of serdesSerialize
andDeserialize
which helped out a lot and following rustsType correctness
felt like a good approach. Sadly I have been struggling to validate the input as well as returning a reasonable error message.I have come across
rocket-validator
which sadly doesn't seem to be maintained anymore and uses a kinda shady fork of theJson
guard from rocket 0.3. It usesvalidator
which seems nice and reminds me of the declarative way ofclass-validator
in TypeScript (if that's good or bad is up to your opinion). So I had a go and tried to implement it as a crate in addition to rocket (keeping things out of rocket which aren't rockets main functionality) rocket-validation.So what's the problem?
Json
in rocket implements theFromData
guard but notValidate
(derived to generate thevalidate
function on the structs). OfferingFromData
andJson
implementations fails due to rusts compiler checking for implementations and hands out an error indicating that there is a duplicate implementation.conflicting implementations of trait
rocket::data::FromData<'>for type
Validated<rocket::serde::json::Json<>>``TLDR;
Validated<Json<T>>
andValidated<T>
are colliding asJson
implementsFromData
, is there any solution to that? rather than baking this into rocket or at least theValidate
derive behind a feature flag.Things I have considered but failed:
Json
=> leaves another forked repository of rocket open in the wildValidate
forT
onJson<T>
=> not allowed due to rusts crate system, would be needed in eitherrocket
orvalidator
crateOne thing I haven't tried due to my lack of knowledge: Use
syn
andquote
to switch betweenT
andJson<T>
at compile time as rust would expand the generics into their own types. This rather seems complicated from what I have seen so I would favor a more state forward solutionI'm grateful for any hints and thank you for your help
Beta Was this translation helpful? Give feedback.
All reactions