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
Serde does not automatically validate deserialized structs. This is annoying because a struct instance that is otherwise guaranteed to be valid by construction can be made invalid via serde round trip. Examples in tofn include GroupPublicInfo and SharePublicInfo.
I spent a long time digging for ergonomic ways to validate serde-deserialized data. 😞 Seems like the best solution is to use deserialize_with as described here serde-rs/serde#1858 (review). Documentation on deserialize_with is sparse---this is all I could find: Field attributes · Serde
If we do go this route then presumably the clean way to do it is for each struct would have a validate() method that is called in two places:
at initial construction (eg. in new())
in deserialize_with
That's a pretty generic pattern---might even be worth a Validator trait or something. Note that this crate exists https://github.com/Keats/validator but at first glance it doesn't seem to meet our needs.
Not sure yet whether we have a strong need for this fix but I can envision a time when we do.
The text was updated successfully, but these errors were encountered:
A pattern I'd recommend for this is having a separate "serde type" where you derive Serialize/Deserialize, and then impl TryFrom to convert from the "serde type" to a validated struct. You can then have deserialize_with decode to the "serde type" and then call TryFrom
Serde does not automatically validate deserialized structs. This is annoying because a struct instance that is otherwise guaranteed to be valid by construction can be made invalid via serde round trip. Examples in
tofn
includeGroupPublicInfo
andSharePublicInfo
.I spent a long time digging for ergonomic ways to validate serde-deserialized data. 😞 Seems like the best solution is to use
deserialize_with
as described here serde-rs/serde#1858 (review). Documentation ondeserialize_with
is sparse---this is all I could find: Field attributes · SerdeIf we do go this route then presumably the clean way to do it is for each struct would have a
validate()
method that is called in two places:new()
)deserialize_with
That's a pretty generic pattern---might even be worth a
Validator
trait or something. Note that this crate exists https://github.com/Keats/validator but at first glance it doesn't seem to meet our needs.Not sure yet whether we have a strong need for this fix but I can envision a time when we do.
The text was updated successfully, but these errors were encountered: