Skip to content
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

How to hide a field name from error messages #2219

Closed
irevoire opened this issue May 18, 2022 · 2 comments
Closed

How to hide a field name from error messages #2219

irevoire opened this issue May 18, 2022 · 2 comments

Comments

@irevoire
Copy link

Is there a way to hide a field akin to what we can do in clap.

Here is my structure;

#[derive(Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct Search {
  query: Option<String>,
  #[serde(default = "false")]
  limit: usize,
  #[serde(default = "false")]
  experimental: bool,
}

And when I receive an unknown field, the following error message is thrown, which is great;

Query deserialize error: unknown field `plouf`, expected one of `query`, `limit`, `experimental`.

But I would like to hide the experimental field from the error message.

I was looking for something like that:

pub struct Search {
  ...
  #[serde(default = "false", hide)]
  experimental: bool,
}

But it doesn’t exist. Did I miss something?

@Mingun
Copy link
Contributor

Mingun commented May 18, 2022

There is not such attribute. I think it would be useless mostly. You can try to utilize an expecting attribute to override default serde error message:

#[derive(Deserialize, Debug)]
#[serde(deny_unknown_fields)]
#[serde(expecting = "expected one of `query`, `limit`")]
pub struct Search {
  query: Option<String>,
  #[serde(default = "false")]
  limit: usize,
  #[serde(default = "false")]
  experimental: bool,
}

But I'm not sure that it would work in your case, because it is only one possible error message

@dtolnay
Copy link
Member

dtolnay commented Jul 9, 2023

Yeah there isn't an attribute for this.

I would recommend handwriting the Deserialize impl for Search to have full control over the error reporting.

@dtolnay dtolnay closed this as completed Jul 9, 2023
@dtolnay dtolnay changed the title How to hide a field How to hide a field name from error messages Jul 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants