Skip to content

Fix: Error when creating index from schema with Uri #206

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
70 changes: 48 additions & 22 deletions src/json_schema/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1048,40 +1048,66 @@ mod tests {
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",
r#""http://example.com""#,
r#""https://example.com/path?query=param#fragment""#,
r#""ftp://ftp.example.com/resource""#,
r#""urn:isbn:0451450523""#,
],
vec![
"http:/example.com", // missing slash
"htp://example.com", // invalid scheme
"http://", // missing host
"example.com", // missing scheme
r#""http:/example.com""#, // missing slash
r#""htp://example.com""#, // invalid scheme
r#""http://""#, // missing host
r#""example.com""#, // missing scheme
]
),
(
r#"{"title": "Bar", "type": "string", "format": "email"}"#,
EMAIL,
vec![
// Valid emails
"[email protected]", // valid
"[email protected]", // valid
"[email protected]", // valid
"[email protected]", // valid
r#""[email protected]""#, // valid
r#""[email protected]""#, // valid
r#""[email protected]""#, // valid
r#""[email protected]""#, // valid
],
vec![
// Invalid emails
"plainaddress", // missing '@' and domain
"@missingusername.com", // missing username
"[email protected]", // leading dot in domain
"username@com", // TLD must have at least 2 characters
"username@example,com", // invalid character in domain
"[email protected]", // leading dot in domain
"[email protected]", // domain cannot start with a hyphen
"[email protected]", // domain cannot end with a hyphen
"[email protected]", // double dot in domain name
"[email protected]", // multiple errors in domain
r#""plainaddress""#, // missing '@' and domain
r#""@missingusername.com""#, // missing username
r#""[email protected]""#, // leading dot in domain
r#""username@com""#, // TLD must have at least 2 characters
r#""username@example,com""#, // invalid character in domain
r#""[email protected]""#, // leading dot in domain
r#""[email protected]""#, // domain cannot start with a hyphen
r#""[email protected]""#, // domain cannot end with a hyphen
r#""[email protected]""#, // double dot in domain name
r#""[email protected]""#, // multiple errors in domain
]
),
// Nested URI and email
(
r#"{
"title": "Test Schema",
"type": "object",
"properties": {
"test_str": {"title": "Test string", "type": "string"},
"test_uri": {"title": "Test URI", "type": "string", "format": "uri"},
"test_email": {"title": "Test email", "type": "string", "format": "email"}
},
"required": ["test_str", "test_uri", "test_email"]
}"#,
format!(
r#"\{{{0}"test_str"{0}:{0}{STRING}{0},{0}"test_uri"{0}:{0}{URI}{0},{0}"test_email"{0}:{0}{EMAIL}{0}\}}"#,
WHITESPACE
).as_str(),
vec![
r#"{ "test_str": "cat", "test_uri": "http://example.com", "test_email": "[email protected]" }"#,
],
vec![
// Invalid URI
r#"{ "test_str": "cat", "test_uri": "http:/example.com", "test_email": "[email protected]" }"#,
// Invalid email
r#"{ "test_str": "cat", "test_uri": "http://example.com", "test_email": "[email protected]" }"#,
]
),

Expand Down
4 changes: 2 additions & 2 deletions src/json_schema/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ pub static TIME: &str = r#""(2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9])(\\.[0-9
// https://datatracker.ietf.org/doc/html/rfc9562 and https://stackoverflow.com/questions/136505/searching-for-uuids-in-text-with-regex
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}""#;
// https://datatracker.ietf.org/doc/html/rfc3986#appendix-B
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 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]+)""#;
// https://www.rfc-editor.org/rfc/rfc5322 and https://stackoverflow.com/questions/13992403/regex-validation-of-email-addresses-according-to-rfc5321-rfc5322
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])+)\])$"#;
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])+)\])""#;

/// Supported format type of the `JsonType::String`.
#[derive(Debug, PartialEq)]
Expand Down