rambler-digital-solutions / actix-web-validator

Rust library for providing validation mechanism to actix-web with Validator crate.
MIT License
101 stars 26 forks source link

Problem using derive Validate #19

Closed Roms1383 closed 3 years ago

Roms1383 commented 3 years ago

Hello ! I'm actually facing an issue while trying to use actix-web-validator.

I probably have overlooked a simple thing, but to be honest I'm still very new to Rust.

issue

When using Validate, e.g. :

// src/models.rs
use serde::Deserialize;
use actix_web_validator::Validate;

#[derive(Debug, Deserialize, Clone, Validate)]
pub struct MyStruct {
  #[validate(length(min = 3, max = 60))]
  pub name: String,
  // ...
}

I get the following error :confused: :

pub trait Validate
The trait that validator_derive implements

failed to resolve: could not find `validator` in `{{root}}`

I naively tried to add the following in the root already :alembic: :

// src/lib.rs
#[macro_use]
extern crate actix_web_validator;
// ...

Unsuccessfully 😅

context

Thanks in advance for the help you can provide :smiley:

Roms1383 commented 3 years ago

Ok for beginners like me reading this, I'm not sure it's the correct way to fix but anyway I got it working by adding crates dependencies to the Cargo.toml :

[dependencies]
# ...
validator = "0.12.0"
validator_derive = "0.12.0"
Roms1383 commented 3 years ago

Or simply :

[dependencies]
# ...
validator = { version = "0.12", features = ["derive"] }
jellybobbin commented 3 years ago

i get same problem.

singulared commented 3 years ago

As I see reexport Validate is a bad idea. https://github.com/Keats/validator/blob/e39ec89f4026e97e3089dabc40e33129a1053a6b/validator_derive/src/lib.rs#L75 Validate macro directly use ::validator crate and reexporting can not help in this situation. The correct way is to add explicit validator dependency into your project as @Roms1383 proposes. I will remove reexporting Validator in the next major release