-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Expand file tree
/
Copy pathmodels.rs
More file actions
117 lines (101 loc) · 3.15 KB
/
models.rs
File metadata and controls
117 lines (101 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#![allow(unused_qualifications)]
use http::HeaderValue;
use validator::Validate;
#[cfg(feature = "server")]
use crate::header;
use crate::{models, types::*};
#[allow(dead_code)]
fn from_validation_error(e: validator::ValidationError) -> validator::ValidationErrors {
let mut errs = validator::ValidationErrors::new();
errs.add("na", e);
errs
}
#[allow(dead_code)]
pub fn check_xss_string(v: &str) -> std::result::Result<(), validator::ValidationError> {
if ammonia::is_html(v) {
std::result::Result::Err(validator::ValidationError::new("xss detected"))
} else {
std::result::Result::Ok(())
}
}
#[allow(dead_code)]
pub fn check_xss_vec_string(v: &[String]) -> std::result::Result<(), validator::ValidationError> {
if v.iter().any(|i| ammonia::is_html(i)) {
std::result::Result::Err(validator::ValidationError::new("xss detected"))
} else {
std::result::Result::Ok(())
}
}
#[allow(dead_code)]
pub fn check_xss_map_string(
v: &std::collections::HashMap<String, String>,
) -> std::result::Result<(), validator::ValidationError> {
if v.keys().any(|k| ammonia::is_html(k)) || v.values().any(|v| ammonia::is_html(v)) {
std::result::Result::Err(validator::ValidationError::new("xss detected"))
} else {
std::result::Result::Ok(())
}
}
#[allow(dead_code)]
pub fn check_xss_map_nested<T>(
v: &std::collections::HashMap<String, T>,
) -> std::result::Result<(), validator::ValidationError>
where
T: validator::Validate,
{
if v.keys().any(|k| ammonia::is_html(k)) || v.values().any(|v| v.validate().is_err()) {
std::result::Result::Err(validator::ValidationError::new("xss detected"))
} else {
std::result::Result::Ok(())
}
}
#[allow(dead_code)]
pub fn check_xss_map<T>(
v: &std::collections::HashMap<String, T>,
) -> std::result::Result<(), validator::ValidationError> {
if v.keys().any(|k| ammonia::is_html(k)) {
std::result::Result::Err(validator::ValidationError::new("xss detected"))
} else {
std::result::Result::Ok(())
}
}
#[derive(Debug, Clone, PartialEq, PartialOrd, serde::Serialize, serde::Deserialize)]
#[cfg_attr(feature = "conversion", derive(frunk::LabelledGeneric))]
pub struct Email(pub String);
impl validator::Validate for Email {
fn validate(&self) -> std::result::Result<(), validator::ValidationErrors> {
std::result::Result::Ok(())
}
}
impl std::convert::From<String> for Email {
fn from(x: String) -> Self {
Email(x)
}
}
impl std::fmt::Display for Email {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl std::str::FromStr for Email {
type Err = std::string::ParseError;
fn from_str(x: &str) -> std::result::Result<Self, Self::Err> {
std::result::Result::Ok(Email(x.to_string()))
}
}
impl std::convert::From<Email> for String {
fn from(x: Email) -> Self {
x.0
}
}
impl std::ops::Deref for Email {
type Target = String;
fn deref(&self) -> &String {
&self.0
}
}
impl std::ops::DerefMut for Email {
fn deref_mut(&mut self) -> &mut String {
&mut self.0
}
}