Skip to content

Commit

Permalink
update regex and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sky-2002 committed Dec 19, 2024
1 parent 3275dd5 commit 266e95a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
34 changes: 16 additions & 18 deletions src/json_schema/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -899,27 +899,25 @@ mod tests {
r#"{"title": "Bar", "type": "string", "format": "email"}"#,
EMAIL,
vec![
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"user@[IPv6:2001:db8::1]",
// Valid emails
"[email protected]", // valid
"[email protected]", // valid
"[email protected]", // valid
"[email protected]", // valid
],
vec![
"plainaddress", // missing '@' and domain
"@missingusername.com", // missing username
"[email protected]", // missing domain name
"username@com", // TLD must have at least 2 characters
"username@example,com", // invalid character in domain
"[email protected]", // leading dot in domain is invalid
"[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
"[email protected]", // invalid IP format
// 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]", // double dot in domain name
"[email protected]", // 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
3 changes: 2 additions & 1 deletion src/json_schema/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ 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-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$";
pub static EMAIL: &str =
r"^[A-Za-z0-9]+(?:[._%+-][A-Za-z0-9]+)*@[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)*\.[A-Za-z]{2,63}$";

#[derive(Debug, PartialEq)]
pub enum FormatType {
Expand Down

0 comments on commit 266e95a

Please sign in to comment.