Skip to content

Commit

Permalink
Email format in JSON schema (#128)
Browse files Browse the repository at this point in the history
Added basic email parsing regex, closes #107
  • Loading branch information
sky-2002 authored Dec 20, 2024
1 parent c55f046 commit 346798c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/json_schema/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -893,8 +893,33 @@ mod tests {
"htp://example.com", // invalid scheme
"http://", // missing host
"example.com", // missing scheme
],
]
),
(
r#"{"title": "Bar", "type": "string", "format": "email"}"#,
EMAIL,
vec![
// Valid emails
"user@example.com", // valid
"user.name+tag+sorting@example.com", // valid
"user_name@example.co.uk", // valid
"user-name@sub.example.com", // valid
],
vec![
// Invalid emails
"plainaddress", // missing '@' and domain
"@missingusername.com", // missing username
"username@.com", // leading dot in domain
"username@com", // TLD must have at least 2 characters
"username@example,com", // invalid character in domain
"username@.example.com", // leading dot in domain
"username@-example.com", // domain cannot start with a hyphen
"username@example-.com", // domain cannot end with a hyphen
"username@example..com", // double dot in domain name
"username@.example..com", // multiple errors in domain
]
)

] {
let json: Value = serde_json::from_str(schema).expect("Can't parse json");
let result = to_regex(&json, None).expect("To regex failed");
Expand Down
4 changes: 4 additions & 0 deletions src/json_schema/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub static DATE: &str = r#""(?:\d{4})-(?:0[1-9]|1[0-2])-(?:0[1-9]|[1-2][0-9]|3[0
pub static TIME: &str = r#""(2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9])(\\.[0-9]+)?(Z)?""#;
pub static UUID: &str = r#""[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}""#;
pub static URI: &str = r#"^(https?|ftp):\/\/([^\s:@]+(:[^\s:@]*)?@)?([a-zA-Z\d.-]+\.[a-zA-Z]{2,}|localhost)(:\d+)?(\/[^\s?#]*)?(\?[^\s#]*)?(#[^\s]*)?$|^urn:[a-zA-Z\d][a-zA-Z\d\-]{0,31}:[^\s]+$"#;
pub static EMAIL: &str = r#"^(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])$"#;

#[derive(Debug, PartialEq)]
pub enum FormatType {
Expand All @@ -43,6 +44,7 @@ pub enum FormatType {
Time,
Uuid,
Uri,
Email,
}

impl FormatType {
Expand All @@ -53,6 +55,7 @@ impl FormatType {
FormatType::Time => TIME,
FormatType::Uuid => UUID,
FormatType::Uri => URI,
FormatType::Email => EMAIL,
}
}

Expand All @@ -64,6 +67,7 @@ impl FormatType {
"time" => Some(FormatType::Time),
"uuid" => Some(FormatType::Uuid),
"uri" => Some(FormatType::Uri),
"email" => Some(FormatType::Email),
_ => None,
}
}
Expand Down

0 comments on commit 346798c

Please sign in to comment.