]> saetta.ns0.it Git - rust/zakform/commitdiff
Implemented validator regex.
authorAndrea Zagli <azagli@libero.it>
Fri, 29 Sep 2023 13:55:12 +0000 (15:55 +0200)
committerAndrea Zagli <azagli@libero.it>
Fri, 29 Sep 2023 13:55:12 +0000 (15:55 +0200)
Cargo.lock
Cargo.toml
po/it.po
src/validators.rs

index 8529bff1cd4aae75ad36f2fcd31c81c842e33828..d156120208b68b74a8204177ba36a76b93dbb599 100644 (file)
@@ -1250,9 +1250,9 @@ dependencies = [
 
 [[package]]
 name = "regex"
-version = "1.9.5"
+version = "1.9.6"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "697061221ea1b4a94a624f67d0ae2bfe4e22b8a17b6a192afb11046542cc8c47"
+checksum = "ebee201405406dbf528b8b672104ae6d6d63e6d118cb10e4d51abbc7b58044ff"
 dependencies = [
  "aho-corasick",
  "memchr",
@@ -1262,9 +1262,9 @@ dependencies = [
 
 [[package]]
 name = "regex-automata"
-version = "0.3.8"
+version = "0.3.9"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "c2f401f4955220693b56f8ec66ee9c78abffd8d1c4f23dc41a23839eb88f0795"
+checksum = "59b23e92ee4318893fa3fe3e6fb365258efbfe6ac6ab30f090cdcbb7aa37efa9"
 dependencies = [
  "aho-corasick",
  "memchr",
@@ -1926,6 +1926,7 @@ dependencies = [
  "config",
  "gettext",
  "lazy_static",
+ "regex",
  "serde",
  "serde_derive",
  "serde_json",
index ae49503c31a61104c49a9cf074ebd89d90d89a91..59194002d6644d5cf8cb4e13ad43ecb1db06caef 100644 (file)
@@ -13,6 +13,7 @@ tera = "1.8.0"
 chrono = { version = "0.4" }
 lazy_static = "1.4.0"
 gettext = "0.4.0"
+regex = "1.5"
 
 [dev-dependencies]
 config = "0.13"
index c00c2b85b9417c5e20bb96a8eb207de21cd1090f..1e197a983f715133898a1e14c18d7310a38d9d3c 100644 (file)
--- a/po/it.po
+++ b/po/it.po
@@ -21,6 +21,8 @@ msgstr ""
 msgid "Mandatory field"
 msgstr "Campo obbligatorio"
 
+msgid "Invalid value"
+msgstr "Valore non valido"
 
 msgid "Invalid date"
 msgstr "Data non valida"
index b549acb03daec689d57e9b5a099b22c80d87c365..92a92986bd85bee0980b53f0bf59d71db9f9ecac 100644 (file)
@@ -1,5 +1,7 @@
 use std::default::Default;
 
+use regex::Regex;
+
 use chrono::NaiveDate;
 
 #[derive(Default)]
@@ -58,6 +60,7 @@ impl ValidatorNotEmpty {
 pub struct ValidatorRegex {
        validator: Validator,
        regex: String,
+       re: Option<Regex>,
 }
 
 impl TValidator for ValidatorRegex {
@@ -74,7 +77,16 @@ impl TValidator for ValidatorRegex {
        }
 
        fn validate(&self, value: &String) -> String {
-               String::from("")
+               match &self.re {
+                       Some(re) => {
+                               if re.is_match(value) {
+                                       String::from("")
+                               } else {
+                                       self.get_message()
+                               }
+                       },
+                       None => { String::from("") },
+               }
        }
 }
 
@@ -82,8 +94,8 @@ impl ValidatorRegex {
        pub fn new(name: &str, regex: &str) -> Self {
                let mut v: ValidatorRegex = Default::default();
                v.validator.name = String::from(name);
-               v.validator.message = String::from("Invalid value");
-               v.regex = String::from(regex);
+               v.validator.message = String::from(super::tr.gettext("Invalid value"));
+               v.set_regex(regex);
 
                v
        }
@@ -94,6 +106,7 @@ impl ValidatorRegex {
 
        pub fn set_regex(&mut self, regex: &str) {
                self.regex = String::from(regex);
+               self.re = Some(Regex::new(regex).unwrap());
        }
 }