-
-
Notifications
You must be signed in to change notification settings - Fork 31
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
serde-wasm-bindgen
doesn't handle serde(deny_unknown_fields)
properly
#39
Comments
Yeah I'm afraid you'll have to manually capture extra fields like here: https://serde.rs/attr-flatten.html#capture-additional-fields The reason is that, unlike Being able to pick only known properties from JS object instead of iterating over its all key-value pairs as a map is a significant perf optimisation, and I wouldn't want to remove it for the relatively rare attribute. Ideally Serde upstream should switch to |
The easiest solution for now might be to use a helper like this: fn deserialize_deny_unknown_fields<'de, T: Deserialize<'de>>(value: JsValue) -> Result<T, serde_wasm_bindgen::Error> {
#[derive(Deserialize)]
struct Wrap<T> {
#[serde(flatten)]
result: T,
#[serde(flatten)]
extra_fields: HashMap<String, serde::de::IgnoredAny>,
}
let Wrap {
result,
extra_fields,
} = serde_wasm_bindgen::from_value(value)?;
if !extra_fields.is_empty() {
return Err(serde::de::Error::custom(format_args!(
"Unexpected fields: {:?}",
extra_fields.keys().collect::<Vec<_>>()
)));
}
Ok(result)
} |
@RReverser thank you for the answer! Will follow your advice. |
An example:
Panics with:
The text was updated successfully, but these errors were encountered: