rambler-digital-solutions / actix-web-validator

Rust library for providing validation mechanism to actix-web with Validator crate.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

validator::traits::Validate is not implemented

kkharji opened this issue · comments

Hello there,

I'm trying out the library to facilitate ease validation error responses.

I've made a struct for create requests:

use serde::{Deserialize, Serialize};
use validator::Validate;

#[derive(Deserialize, Validate, Debug)]
pub struct CreateUser {
    #[validate(length(min = 3))]
    pub first_name: String,
    #[validate(length(min = 3))]
    pub last_name: String,
    #[validate(email)]
    pub email: String,
    pub password: String,
}

then in my handler

use actix_web_validator::Json;
use actix_web::error::HttpError;
use actix_web::HttpResponse;

#[actix_web::post("/users")]
pub async fn create(req: Json<CreateUser>) -> Result<HttpResponse, HttpError> {
    todo!()
}

but I get the following compile error

the trait `validator::traits::Validate` is not implemented for `CreateUser`

Can you please point what I'm doing wrong. Thanks

Hello @tami5. I think you missed derive features for serde and validator crates.

Cargo.toml
serde = { version = "1", features = ["derive"] }
validator = { version = "0.14", features = ["derive"] }

🤔 I have them both

validator = { version = "0.12", features = ["derive"] }
serde = { version ="1.0.136", features = ["derive"]  }

Oh my bad, had an outdate version pf validator.

Thanks @singulared, awesome library looking forward to experiment more with it 🙏