icydb_base/validator/
web.rs

1use crate::{core::traits::Validator, prelude::*};
2
3///
4/// MimeType
5///
6
7#[validator]
8pub struct MimeType {}
9
10impl Validator<str> for MimeType {
11    fn validate(&self, s: &str) -> Result<(), String> {
12        let parts: Vec<&str> = s.split('/').collect();
13        if parts.len() != 2 {
14            return Err(format!("MIME type '{s}' must contain exactly one '/'"));
15        }
16
17        let is_valid_part = |part: &str| {
18            !part.is_empty()
19                && part
20                    .chars()
21                    .all(|c| c.is_ascii_alphanumeric() || "+.-".contains(c))
22        };
23
24        if !is_valid_part(parts[0]) || !is_valid_part(parts[1]) {
25            return Err(format!(
26                "MIME type '{s}' contains invalid characters; only alphanumeric, '+', '-', '.' allowed"
27            ));
28        }
29
30        Ok(())
31    }
32}
33
34///
35/// Url
36///
37
38#[validator]
39pub struct Url {}
40
41impl Validator<str> for Url {
42    fn validate(&self, s: &str) -> Result<(), String> {
43        // Very basic check — can be expanded
44        if s.starts_with("http://") || s.starts_with("https://") {
45            Ok(())
46        } else {
47            Err(format!("URL '{s}' must start with 'http://' or 'https://'"))
48        }
49    }
50}