schemars icon indicating copy to clipboard operation
schemars copied to clipboard

Set regex for Array String items

Open goddtriffin opened this issue 4 years ago • 0 comments
trafficstars

For the following code:

#[derive(Serialize, Deserialize, JsonSchema)]
pub struct Team {
    /// # Users
    /// A list of users.
    #[schemars(regex(pattern = r"^(.*)$"))]
    pub users: Vec<String>
}

This is the output I currently see for the users field:

"users": {
    "title": "Users",
    "description": "A list users.",
    "type": "array",
    "items": {
        "type": "string"
    }
},

As you can see, the regex is simply getting omitted entirely.

What I would like to see is:

"users": {
    "title": "Users",
    "description": "A list users.",
    "type": "array",
    "items": {
        "pattern": "^(.*)$",
        "type": "string"
    }
},

Since regex doesn't make sense for an array, it should bubble down to the contained String type.

I would also expect this to work when setting length(max = 39) on the users field, where it isn't applied to the array, but to the underlying String.

goddtriffin avatar Sep 22 '21 23:09 goddtriffin