valico/json_schema/validators/
formats.rs

1use serde_json::{Value};
2use chrono;
3use std::net;
4use uuid;
5use url;
6use publicsuffix::List;
7
8use super::super::errors;
9use super::super::scope;
10
11#[allow(missing_copy_implementations)]
12pub struct DateTime;
13
14impl super::Validator for DateTime {
15    fn validate(&self, val: &Value, path: &str, _scope: &scope::Scope) -> super::ValidationState {
16        let string = nonstrict_process!(val.as_str(), path);
17
18        match chrono::DateTime::parse_from_rfc3339(string) {
19            Ok(_) => super::ValidationState::new(),
20            Err(_) => {
21                val_error!(
22                    errors::Format {
23                        path: path.to_string(),
24                        detail: "Malformed date time".to_string()
25                    }
26                )
27            }
28        }
29    }
30}
31
32#[allow(missing_copy_implementations)]
33pub struct Email;
34
35impl super::Validator for Email {
36    fn validate(&self, val: &Value, path: &str, _scope: &scope::Scope) -> super::ValidationState {
37        let string = nonstrict_process!(val.as_str(), path);
38
39        match List::empty().parse_email(string) {
40            Ok(_) => super::ValidationState::new(),
41            Err(_) => {
42                val_error!(
43                    errors::Format {
44                        path: path.to_string(),
45                        detail: "Malformed email address".to_string()
46                    }
47                )
48            }
49        }
50    }
51}
52
53#[allow(missing_copy_implementations)]
54pub struct Hostname;
55
56impl super::Validator for Hostname {
57    fn validate(&self, val: &Value, path: &str, _scope: &scope::Scope) -> super::ValidationState {
58        let string = nonstrict_process!(val.as_str(), path);
59
60        match List::empty().parse_domain(string) {
61            Ok(_) => super::ValidationState::new(),
62            Err(_) => {
63                val_error!(
64                    errors::Format {
65                        path: path.to_string(),
66                        detail: "Malformed email address".to_string()
67                    }
68                )
69            }
70        }
71    }
72}
73
74#[allow(missing_copy_implementations)]
75pub struct Ipv4;
76
77impl super::Validator for Ipv4 {
78    fn validate(&self, val: &Value, path: &str, _scope: &scope::Scope) -> super::ValidationState {
79        let string = nonstrict_process!(val.as_str(), path);
80
81        match string.parse::<net::Ipv4Addr>() {
82            Ok(_) => super::ValidationState::new(),
83            Err(_) => {
84                val_error!(
85                    errors::Format {
86                        path: path.to_string(),
87                        detail: "Malformed IP address".to_string()
88                    }
89                )
90            }
91        }
92    }
93}
94
95#[allow(missing_copy_implementations)]
96pub struct Ipv6;
97
98impl super::Validator for Ipv6 {
99    fn validate(&self, val: &Value, path: &str, _scope: &scope::Scope) -> super::ValidationState {
100        let string = nonstrict_process!(val.as_str(), path);
101
102        match string.parse::<net::Ipv6Addr>() {
103            Ok(_) => super::ValidationState::new(),
104            Err(_) => {
105                val_error!(
106                    errors::Format {
107                        path: path.to_string(),
108                        detail: "Malformed IP address".to_string()
109                    }
110                )
111            }
112        }
113    }
114}
115
116#[allow(missing_copy_implementations)]
117pub struct Uuid;
118
119impl super::Validator for Uuid {
120    fn validate(&self, val: &Value, path: &str, _scope: &scope::Scope) -> super::ValidationState {
121        let string = nonstrict_process!(val.as_str(), path);
122
123        match string.parse::<uuid::Uuid>() {
124            Ok(_) => super::ValidationState::new(),
125            Err(err) => {
126                val_error!(
127                    errors::Format {
128                        path: path.to_string(),
129                        detail: format!("Malformed UUID: {:?}", err)
130                    }
131                )
132            }
133        }
134    }
135}
136
137#[allow(missing_copy_implementations)]
138pub struct Uri;
139
140impl super::Validator for Uri {
141    fn validate(&self, val: &Value, path: &str, _scope: &scope::Scope) -> super::ValidationState {
142        let string = nonstrict_process!(val.as_str(), path);
143
144        match url::Url::parse(string) {
145            Ok(_) => super::ValidationState::new(),
146            Err(err) => {
147                val_error!(
148                    errors::Format {
149                        path: path.to_string(),
150                        detail: format!("Malformed URI: {}", err)
151                    }
152                )
153            }
154        }
155    }
156}