aboutsummaryrefslogtreecommitdiff
path: root/src/validation
diff options
context:
space:
mode:
Diffstat (limited to 'src/validation')
-rw-r--r--src/validation/category.rs18
-rw-r--r--src/validation/income.rs22
-rw-r--r--src/validation/login.rs11
-rw-r--r--src/validation/mod.rs5
-rw-r--r--src/validation/payment.rs25
-rw-r--r--src/validation/utils.rs54
6 files changed, 135 insertions, 0 deletions
diff --git a/src/validation/category.rs b/src/validation/category.rs
new file mode 100644
index 0000000..7b1b5d5
--- /dev/null
+++ b/src/validation/category.rs
@@ -0,0 +1,18 @@
+use std::collections::HashMap;
+
+use crate::model::category::{Create, Update};
+use crate::validation::utils::*;
+
+pub fn create(form: &HashMap<String, String>) -> Option<Create> {
+ Some(Create {
+ name: non_empty(form, "name")?,
+ color: color(form, "color")?,
+ })
+}
+
+pub fn update(form: &HashMap<String, String>) -> Option<Update> {
+ Some(Update {
+ name: non_empty(form, "name")?,
+ color: color(form, "color")?,
+ })
+}
diff --git a/src/validation/income.rs b/src/validation/income.rs
new file mode 100644
index 0000000..972e42a
--- /dev/null
+++ b/src/validation/income.rs
@@ -0,0 +1,22 @@
+use std::collections::HashMap;
+
+use crate::model::income::{Create, Update};
+use crate::validation::utils::*;
+
+pub fn create(form: &HashMap<String, String>) -> Option<Create> {
+ Some(Create {
+ user_id: parse::<i64>(form, "user_id")?,
+ amount: parse::<i64>(form, "amount")?,
+ month: parse::<u32>(form, "month")?,
+ year: parse::<i32>(form, "year")?,
+ })
+}
+
+pub fn update(form: &HashMap<String, String>) -> Option<Update> {
+ Some(Update {
+ user_id: parse::<i64>(form, "user_id")?,
+ amount: parse::<i64>(form, "amount")?,
+ month: parse::<u32>(form, "month")?,
+ year: parse::<i32>(form, "year")?,
+ })
+}
diff --git a/src/validation/login.rs b/src/validation/login.rs
new file mode 100644
index 0000000..e40bb23
--- /dev/null
+++ b/src/validation/login.rs
@@ -0,0 +1,11 @@
+use std::collections::HashMap;
+
+use crate::model::login::Login;
+use crate::validation::utils::*;
+
+pub fn login(form: &HashMap<String, String>) -> Option<Login> {
+ Some(Login {
+ email: non_empty(form, "email")?,
+ password: non_empty(form, "password")?,
+ })
+}
diff --git a/src/validation/mod.rs b/src/validation/mod.rs
new file mode 100644
index 0000000..181abc7
--- /dev/null
+++ b/src/validation/mod.rs
@@ -0,0 +1,5 @@
+pub mod category;
+pub mod income;
+pub mod login;
+pub mod payment;
+pub mod utils;
diff --git a/src/validation/payment.rs b/src/validation/payment.rs
new file mode 100644
index 0000000..36aa852
--- /dev/null
+++ b/src/validation/payment.rs
@@ -0,0 +1,25 @@
+use std::collections::HashMap;
+
+use crate::model::payment::{Create, Update};
+use crate::validation::utils::*;
+
+pub fn create(form: &HashMap<String, String>) -> Option<Create> {
+ Some(Create {
+ name: non_empty(form, "name")?,
+ cost: parse::<i64>(form, "cost")?,
+ user_id: parse::<i64>(form, "user_id")?,
+ category_id: parse::<i64>(form, "category_id")?,
+ date: date(form, "date")?,
+ frequency: frequency(form, "frequency")?,
+ })
+}
+
+pub fn update(form: &HashMap<String, String>) -> Option<Update> {
+ Some(Update {
+ name: non_empty(form, "name")?,
+ cost: parse::<i64>(form, "cost")?,
+ user_id: parse::<i64>(form, "user_id")?,
+ category_id: parse::<i64>(form, "category_id")?,
+ date: date(form, "date")?,
+ })
+}
diff --git a/src/validation/utils.rs b/src/validation/utils.rs
new file mode 100644
index 0000000..4bff40a
--- /dev/null
+++ b/src/validation/utils.rs
@@ -0,0 +1,54 @@
+use chrono::NaiveDate;
+use std::collections::HashMap;
+use std::str::FromStr;
+
+use crate::model::frequency::Frequency;
+
+pub fn non_empty(
+ form: &HashMap<String, String>,
+ field: &str,
+) -> Option<String> {
+ let s = form.get(field)?.trim();
+ if s.is_empty() {
+ None
+ } else {
+ Some(s.to_string())
+ }
+}
+
+pub fn parse<T: FromStr>(
+ form: &HashMap<String, String>,
+ field: &str,
+) -> Option<T> {
+ let s = form.get(field)?;
+ s.parse::<T>().ok()
+}
+
+pub fn date(form: &HashMap<String, String>, field: &str) -> Option<NaiveDate> {
+ let s = form.get(field)?;
+ NaiveDate::parse_from_str(s, "%Y-%m-%d").ok()
+}
+
+pub fn frequency(
+ form: &HashMap<String, String>,
+ field: &str,
+) -> Option<Frequency> {
+ let s = form.get(field)?;
+ Frequency::from_str(s).ok()
+}
+
+pub fn color(form: &HashMap<String, String>, field: &str) -> Option<String> {
+ let s = form.get(field)?;
+ if s.len() == 7
+ && &s[0..1] == "#"
+ && s[1..]
+ .to_string()
+ .into_bytes()
+ .into_iter()
+ .all(|c| c.is_ascii_hexdigit())
+ {
+ Some(s.to_string())
+ } else {
+ None
+ }
+}