somehowchris / rocket-validation

A guard to validate data received by rocket via validator
16 stars 4 forks source link

Could not find validator in the list of imported crates #14

Closed remykarem closed 1 year ago

remykarem commented 2 years ago

I get the following error when going through the json example:

 --> src/main.rs:7:41
  |
7 | #[derive(Debug, Deserialize, Serialize, Validate)]
  |                                         ^^^^^^^^ could not find `validator` in the list of imported crates

example from https://github.com/somehowchris/rocket-validation/tree/main/examples/json-validation

#[macro_use]
extern crate rocket;

use rocket::serde::{json::Json, Deserialize, Serialize};
use rocket_validation::{Validated, Validate};

#[derive(Debug, Deserialize, Serialize, Validate)]
#[serde(crate = "rocket::serde")]
pub struct HelloData {
    #[validate(length(min = 1))]
    name: String,
    #[validate(range(min = 0, max = 100))]
    age: u8,
}

...

But after I added validator as a dependency, it compiles.

Should we update the docs to include this dependency?

somehowchris commented 2 years ago

Thanks a lot for the issue. Somehow I didn't yet come across this issue (as I usually needed to use validator anyways) but it's an interesting issue.

It originates as the Validate derive will expand into code like this

impl ::validator::Validate for HelloData {
    fn validate(&self) -> ::std::result::Result<(), ::validator::ValidationErrors> {
        use ::validator::ValidateArgs;
        self.validate_args(())
    }
}

Which assumes that there is a module called validator aka the crate itself. It's like the serde one where rocket reexports the crate so we can use #[serde(crate = "rocket::serde")].

Can't really see any other way than adding validator as a direct dependency right now. Will look into this tomorrow.

remykarem commented 2 years ago

Great, thanks!

somehowchris commented 1 year ago

I'm so sorry. Got caught up in some stuff.

Added a line to the readme

remykarem commented 1 year ago

Awesome, cheers!