Skip to content

Commit

Permalink
Support URI format (dottxt-ai#129)
Browse files Browse the repository at this point in the history
Adds URI parsing code and tests

Refer dottxt-ai#127
  • Loading branch information
sky-2002 committed Dec 19, 2024
1 parent 60b5483 commit b7f63ba
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/json_schema/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,25 @@ mod tests {
"[email protected]", // double dot in domain name
"[email protected]", // multiple errors in domain
"[email protected]", // invalid IP format
]
),
// ==========================================================
// URI Format
// ==========================================================
(
r#"{"title": "Foo", "type": "string", "format": "uri"}"#,
URI,
vec![
"http://example.com",
"https://example.com/path?query=param#fragment",
"ftp://ftp.example.com/resource",
"urn:isbn:0451450523",
],
vec![
"http:/example.com", // missing slash
"htp://example.com", // invalid scheme
"http://", // missing host
"example.com", // missing scheme
],
),
] {
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 @@ -36,6 +36,7 @@ pub static TIME: &str = r#""(2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9])(\\.[0-9
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-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$";
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]+$"#;

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

impl FormatType {
Expand All @@ -56,6 +58,7 @@ impl FormatType {
FormatType::Uuid => UUID,
FormatType::Uri => URI,
FormatType::Email => EMAIL,
FormatType::Uri => URI,
}
}

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

0 comments on commit b7f63ba

Please sign in to comment.