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

Validation not invoked on Json #2757

Closed
4 tasks done
vlio20 opened this issue Mar 24, 2024 · 6 comments
Closed
4 tasks done

Validation not invoked on Json #2757

vlio20 opened this issue Mar 24, 2024 · 6 comments
Labels
no bug The reported bug was confirmed nonexistent

Comments

@vlio20
Copy link

vlio20 commented Mar 24, 2024

Rocket Version

0.5.0

Operating System

macos

Rust Toolchain Version

rustc 1.76.0 (07dca489a 2024-02-04)

What happened?

Custom validation is never invoked.

Test Case

I have the following code:

#[derive(Debug, Deserialize, FromForm)]
#[serde(crate = "rocket::serde")]
pub struct CreateUserDto<'r> {
    #[field(validate = email_validation())]
    email: &'r str,
    password: &'r str,
}

fn email_validation(email: &str) -> Result<()> {
    let re: Regex = Regex::new(r"^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$").unwrap();

    if !re.is_match(email) {
        Err(Error::validation("invalid email address"))?;
    }

    Ok(())
}

In that code I am trying to validate the email field according to a given regex. For some reason the email_validation function is never called.

Any idea why? I was following the Rocket documentation as seen here: https://api.rocket.rs/master/rocket/form/validate/#custom-validation



### Log Output

```shell
No errors when running the above code. The validation is never executed - validated with wrong input and with with a debugger breakpoint.

Additional Context

No response

System Checks

  • My bug report relates to functionality.
  • I have tested against the latest Rocket release or a recent git commit.
  • I have tested against the latest stable rustc toolchain.
  • I was unable to find this issue previously reported.
@vlio20 vlio20 added the triage A bug report being investigated label Mar 24, 2024
@SergioBenitez
Copy link
Member

Invoked at what point? Your code doesn't include any routes.

@vlio20
Copy link
Author

vlio20 commented Mar 24, 2024

@SergioBenitez, this is the route:

#[post("/", data = "<input>")]
fn creart_user(input: Json<CreateUserDto>) {
    let user = input.into_inner();
    println!("email {0}, password: {1}", user.email, user.password);
}

It seems like the validations are not running on Json. They do run on Form.

@vlio20 vlio20 changed the title custom validation not invoked Validation not invoked on Json Mar 24, 2024
@vlio20
Copy link
Author

vlio20 commented Mar 24, 2024

Maybe it is related to the following:
#2110

@SergioBenitez
Copy link
Member

There's likely no bug here. You're using the Json type which works through the Deserialize trait. On the other hand, the field attribute you're using to validate is part of the FromForm trait and is not involved during deserialization.

@vlio20
Copy link
Author

vlio20 commented Mar 25, 2024

Is there any way to make the Json type to preform the Field validations?

@SergioBenitez
Copy link
Member

Not at the moment. They simply work through different mechanisms. I do have a plan to align the different "data" processors to make that happen, but that will be a larger effort.

@SergioBenitez SergioBenitez added no bug The reported bug was confirmed nonexistent and removed triage A bug report being investigated labels Apr 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no bug The reported bug was confirmed nonexistent
Projects
None yet
Development

No branches or pull requests

2 participants