Skip to content

Fields that are string slices fail to validate when containing newlines #41

@LostLuma

Description

@LostLuma

Using string slices in a validated struct instead of strings causes a validation error if the field sent by the client contains newlines.

Here is the struct I noticed this issue on:

#[derive(Deserialize, Serialize, Validate)]
pub struct CreateFile<'r> {
    #[validate(length(min = 1, max = 32))]
    name: Option<&'r str>,
    #[validate(length(min = 1, max = 300_000))]
    content: &'r str,
}

Sending a newline character in either field causes the server to respond with the following error:

{
    "code": 422,
    "message": "Unprocessable Entity. The request was well-formed but was unable to be followed due to semantic errors.",
    "errors": null
}

Replacing the content field type with String instead fixes the issue, which I did for the time being.
I'd however like to abstain from doing so since it is, as far as I'm aware, less efficient and Rocket discourages doing so.


This issue is not present when using the validator crate on its own (neither version v0.16.1 nor v0.18.1 are affected).

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