GREsau / schemars

Generate JSON Schema documents from Rust code
https://graham.cool/schemars/
MIT License
820 stars 227 forks source link

expected validate path attribute to be a string: `path = "..."` #302

Closed forest1102 closed 2 months ago

forest1102 commented 4 months ago

Because of syntax difference in regex validation between Validator and schemars, compile error occurs when using regex.
In validators,

    #[validate(regex(path = *RE_TWO_CHARS))]

While schemars,

    #[validate(regex(path = "RE_TWO_CHARS"))]

Sample:

static RE_TWO_CHARS: Lazy<Regex> = Lazy::new(|| Regex::new(r"[a-z]{2}$").unwrap());

#[derive(Deserialize, Debug, Validate, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct SampleJdto {
    #[validate(regex(path = *RE_TWO_CHARS))]
    pub project_name: String,
}
GREsau commented 2 months ago

This is fixed in schemars 1.0.0-alpha.13 🙂

The schema generated from the sample given in this issue is now:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "SampleJdto",
  "type": "object",
  "properties": {
    "projectName": {
      "type": "string",
      "pattern": "[a-z]{2}$"
    }
  },
  "required": [
    "projectName"
  ]
}