jsonschema_valid/
format.rs

1use std::net::Ipv4Addr;
2use std::net::Ipv6Addr;
3use std::str::FromStr;
4
5use chrono::DateTime;
6use regex::Regex;
7use url::{Host, Url};
8
9use crate::config::Config;
10
11pub type FormatChecker = fn(cfg: &Config, value: &str) -> bool;
12
13pub fn email(_cfg: &Config, value: &str) -> bool {
14    value.contains('@')
15}
16
17pub fn ipv4(_cfg: &Config, value: &str) -> bool {
18    Ipv4Addr::from_str(value).is_ok()
19}
20
21pub fn ipv6(_cfg: &Config, value: &str) -> bool {
22    Ipv6Addr::from_str(value).is_ok()
23}
24
25pub fn hostname(_cfg: &Config, value: &str) -> bool {
26    Host::parse(value).is_ok()
27}
28
29pub fn uri(_cfg: &Config, value: &str) -> bool {
30    Url::parse(value).is_ok()
31}
32
33pub fn uri_reference(_cfg: &Config, _value: &str) -> bool {
34    true
35    // TODO: This is not correct
36    // Url::parse(value).is_ok()
37}
38
39pub fn iri(_cfg: &Config, value: &str) -> bool {
40    iri_string::types::IriAbsoluteStr::new(value).is_ok()
41}
42
43pub fn iri_reference(_cfg: &Config, value: &str) -> bool {
44    iri_string::types::IriAbsoluteStr::new(value).is_ok()
45}
46
47pub fn datetime(_cfg: &Config, value: &str) -> bool {
48    DateTime::parse_from_rfc3339(value).is_ok()
49}
50
51pub fn regex(_cfg: &Config, value: &str) -> bool {
52    Regex::new(value).is_ok()
53}
54
55pub fn date(_cfg: &Config, value: &str) -> bool {
56    DateTime::parse_from_str(value, "%Y-%m-%d").is_ok()
57}
58
59pub fn time(_cfg: &Config, value: &str) -> bool {
60    DateTime::parse_from_str(value, "%H:%M:%S").is_ok()
61}
62
63pub fn json_pointer(_cfg: &Config, value: &str) -> bool {
64    value.parse::<json_pointer::JsonPointer<_, _>>().is_ok()
65}
66
67pub fn uri_template(_cfg: &Config, _value: &str) -> bool {
68    // It seems like pretty much anything can be a URI template
69    true
70}