summaryrefslogtreecommitdiff
path: root/server/src/models
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2022-08-22 23:03:59 +0200
committerSanto Cariotti <santo@dcariotti.me>2022-08-22 23:03:59 +0200
commitea2ac88a83b6de15beb8b9727f88d498a56fb5b3 (patch)
tree826154c0118505f73fea21c80435b4c6e6a4e71e /server/src/models
parent3092a758f382680a56e7343b036468db26111f98 (diff)
Add validation
Diffstat (limited to 'server/src/models')
-rw-r--r--server/src/models/user.rs10
1 files changed, 6 insertions, 4 deletions
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<bool>,
}
@@ -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?;