Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support URI format #129

Merged
merged 7 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/json_schema/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,25 @@ mod tests {
],
vec!["this isnt valid json"],
),
// ==========================================================
// 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
],
),
] {
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 @@ -34,13 +34,15 @@ pub static DATE_TIME: &str = r#""(-?(?:[1-9][0-9]*)?[0-9]{4})-(1[0-2]|0[1-9])-(3
pub static DATE: &str = r#""(?:\d{4})-(?:0[1-9]|1[0-2])-(?:0[1-9]|[1-2][0-9]|3[0-1])""#;
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]+$"#;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would be curious to see a pointer to the specifications where most of these regexes are defined, as I wouldn't be able to confirm later with my own eyeballs that they're 100% correct!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yvan-sraka I am also searching for specifications where regexes are defined (because same is the case with my eyeballs 👀 )


#[derive(Debug, PartialEq)]
pub enum FormatType {
DateTime,
Date,
Time,
Uuid,
Uri,
}

impl FormatType {
Expand All @@ -50,6 +52,7 @@ impl FormatType {
FormatType::Date => DATE,
FormatType::Time => TIME,
FormatType::Uuid => UUID,
FormatType::Uri => URI,
}
}

Expand All @@ -60,6 +63,7 @@ impl FormatType {
"date" => Some(FormatType::Date),
"time" => Some(FormatType::Time),
"uuid" => Some(FormatType::Uuid),
"uri" => Some(FormatType::Uri),
_ => None,
}
}
Expand Down
Loading