From ea2ac88a83b6de15beb8b9727f88d498a56fb5b3 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Mon, 22 Aug 2022 23:03:59 +0200 Subject: Add validation --- server/src/models/user.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'server/src/models') diff --git a/server/src/models/user.rs b/server/src/models/user.rs index 9545fac..76cb4b5 100644 --- a/server/src/models/user.rs +++ b/server/src/models/user.rs @@ -2,11 +2,14 @@ use crate::db::get_client; use crate::errors::AppError; use serde::{Deserialize, Serialize}; +use validator::Validate; -#[derive(Deserialize, Serialize)] +#[derive(Deserialize, Serialize, Validate)] pub struct User { id: i32, + #[validate(length(min = 1, message = "Can not be empty"))] email: String, + #[validate(length(min = 8, message = "Must be min 8 chars length"))] password: String, is_staff: Option, } @@ -26,11 +29,10 @@ pub struct UserCreate { impl User { pub fn new(email: String, password: String) -> Self { - let crypted_password = sha256::digest(password); Self { id: 0, email, - password: crypted_password, + password, is_staff: Some(false), } } @@ -45,7 +47,7 @@ impl User { RETURNING id, email, is_staff "#, user.email, - user.password + sha256::digest(user.password) ) .fetch_one(pool) .await?; -- cgit v1.2.3-18-g5258