Skip to content

Linter: Regex serialization for JsonSchema #15203

@connorshea

Description

@connorshea

There are more than a dozen rules with config options which cannot be serialized using serde/schemars because they use lazy_regex, see this PR for an example of the problem: #15159

Code like this will not work because of using Regex with JsonSchema:

#[derive(Debug, Deserialize, Clone, Serialize, JsonSchema, Default)]
#[serde(rename_all = "camelCase", default)]
pub struct ParamNamesConfig {
    /// Regex pattern used to validate the `resolve` parameter name. If provided, this pattern
    /// is used instead of the default `^_?resolve$` check.
    resolve_pattern: Option<Regex>,
    /// Regex pattern used to validate the `reject` parameter name. If provided, this pattern
    /// is used instead of the default `^_?reject$` check.
    reject_pattern: Option<Regex>,
}

See also GREsau/schemars#476, which potentially solves the problem upstream? Although oxc uses a schemars fork currently, so we'll need to pull it in ourselves regardless. (Unfortunately this will require some rework to make it work with the older schemars version we're on because the schemars API was completely changed, and anyway we're using lazy_regex which is distinct from Regex, so it may not be necessarily compatible with this usage)

There is also a SerdeRegexWrapper implemented here, which could potentially be extracted as a solution:

/// A wrapper type which implements `Serialize` and `Deserialize` for
/// types involving `Regex`
#[derive(Debug, Clone, Eq, Hash, PartialEq)]
pub struct SerdeRegexWrapper<T>(pub T);
impl std::ops::Deref for SerdeRegexWrapper<Regex> {
type Target = Regex;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<'de> Deserialize<'de> for SerdeRegexWrapper<Regex> {
fn deserialize<D>(d: D) -> Result<SerdeRegexWrapper<Regex>, D::Error>
where
D: Deserializer<'de>,
{
let s = <Cow<str>>::deserialize(d)?;
match s.parse() {
Ok(regex) => Ok(SerdeRegexWrapper(regex)),
Err(err) => Err(D::Error::custom(err)),
}
}
}

This will block the completion of #14743 until it's solved.

This has also required the usage of schemars(skip) on some config options, such as in no-unused-vars. This results in the options not showing up in the generated documentation on the website. This should be fixed after this issue is resolved.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions