Skip to content

Commit

Permalink
Improve URL pattern matching (#2248)
Browse files Browse the repository at this point in the history
* Improve URL pattern matching

* Add more url matching tests
  • Loading branch information
zero0cool0 authored Dec 7, 2023
1 parent a77db05 commit fab5e41
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Source/Validations/RuleRegExp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import Foundation

public enum RegExprPattern: String {
case EmailAddress = "^[_A-Za-z0-9-+!?#$%'`*/=~^{}|]+(\\.[_A-Za-z0-9-+!?#$%'`*/=~^{}|]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(\\.[A-Za-z‌​]{2,})$"
case URL = "((https|http)://)((\\w|-)+)(([.]|[/])((\\w|-)+))+([/?#]\\S*)?"
case URL = "^(?:(?:http|https)://)(?:\\S+(?::\\S*)?@)?(?:(?:(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[0-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,})))|localhost)(?::\\d{2,5})?(?:(/|\\?|#)[^\\s]*)?$"
case ContainsNumber = ".*\\d.*"
case ContainsCapital = "^.*?[A-Z].*?$"
case ContainsLowercase = "^.*?[a-z].*?$"
Expand Down
7 changes: 6 additions & 1 deletion Tests/ValidationsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,14 @@ class ValidationsTests: XCTestCase {
XCTAssertNil(urlRule.isValid(value: nil))
XCTAssertNil(urlRule.isValid(value: URL(string: "")))
XCTAssertNil(urlRule.isValid(value: URL(string: "http://example.com")))
XCTAssertNil(urlRule.isValid(value: URL(string: "http://to.co")))
XCTAssertNil(urlRule.isValid(value: URL(string: "https://example.com/path/to/file.ext?key=value#location")))

XCTAssertNil(urlRule.isValid(value: URL(string: "https://example.com:8080/path/to/file.ext?key=value#location")))
XCTAssertNil(urlRule.isValid(value: URL(string: "https://localhost")))
XCTAssertNil(urlRule.isValid(value: URL(string: "https://localhost:8080")))

XCTAssertNotNil(urlRule.isValid(value: URL(string: "example.com")))
XCTAssertNotNil(urlRule.isValid(value: URL(string: "www.example.com")))
XCTAssertNotNil(urlRule.isValid(value: URL(string: "http://")))
}
}

0 comments on commit fab5e41

Please sign in to comment.