Skip to content

Commit 4e03300

Browse files
authored
Add URLValidationRule implementation (#25)
* Add `URLValidationRule` implementation * Write unit tests for `URLValidationRule` * Update `README.md` * Update `CHANGELOG.md`
1 parent 304abb5 commit 4e03300

File tree

4 files changed

+157
-0
lines changed

4 files changed

+157
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ All notable changes to this project will be documented in this file.
1111
#### Added
1212
- Add `dependabot.yml`
1313
- Added in Pull Request [#21](https://github.com/space-code/validator/pull/21).
14+
- Add `URLValidationRule`.
15+
- Added in Pull Request [#25](https://github.com/space-code/validator/pull/25).
1416

1517
#### Updated
1618
- Update `Mintfile`

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ struct ContentView: View {
199199
| **PrefixValidationRule** | To validate whether a string contains a prefix |
200200
| **SuffixValidationRule** | To validate whether a string contains a suffix |
201201
| **RegexValidationRule** | To validate if a pattern is matched |
202+
| **URLValidationRule** | To validate whether a string contains a URL |
202203

203204
## Custom Validation Rules
204205

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//
2+
// Validator
3+
// Copyright © 2025 Space Code. All rights reserved.
4+
//
5+
6+
import Foundation
7+
8+
/// A url validation rule.
9+
public struct URLValidationRule: IValidationRule {
10+
// MARK: Types
11+
12+
public typealias Input = String
13+
14+
// MARK: Properties
15+
16+
/// The validation error.
17+
public let error: IValidationError
18+
19+
// MARK: Initialization
20+
21+
public init(error: IValidationError) {
22+
self.error = error
23+
}
24+
25+
// MARK: IValidationRule
26+
27+
public func validate(input: String) -> Bool {
28+
guard let url = URL(string: input) else { return false }
29+
return url.isFileURL || (url.host != nil && url.scheme != nil)
30+
}
31+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
//
2+
// Validator
3+
// Copyright © 2025 Space Code. All rights reserved.
4+
//
5+
6+
import ValidatorCore
7+
import XCTest
8+
9+
// MARK: - URLValidationRuleTests
10+
11+
final class URLValidationRuleTests: XCTestCase {
12+
// MARK: - Properties
13+
14+
private var sut: URLValidationRule!
15+
16+
// MARK: - Setup
17+
18+
override func setUp() {
19+
super.setUp()
20+
sut = URLValidationRule(error: String.error)
21+
}
22+
23+
override func tearDown() {
24+
sut = nil
25+
super.tearDown()
26+
}
27+
28+
// MARK: - Tests
29+
30+
func test_validate_validURL_shouldReturnTrue() {
31+
// given
32+
let url = "https://google.com"
33+
34+
// when
35+
let result = sut.validate(input: url)
36+
37+
// then
38+
XCTAssertTrue(result)
39+
}
40+
41+
func test_validate_missingScheme_shouldReturnFalse() {
42+
// given
43+
let url = "google.com"
44+
45+
// when
46+
let result = sut.validate(input: url)
47+
48+
// then
49+
XCTAssertFalse(result)
50+
}
51+
52+
func test_validate_missingHost_shouldReturnFalse() {
53+
// given
54+
let url = "https://"
55+
56+
// when
57+
let result = sut.validate(input: url)
58+
59+
// then
60+
XCTAssertFalse(result)
61+
}
62+
63+
func test_validate_emptyString_shouldReturnFalse() {
64+
// given
65+
let url = ""
66+
67+
// when
68+
let result = sut.validate(input: url)
69+
70+
// then
71+
XCTAssertFalse(result)
72+
}
73+
74+
func test_validate_whitespaceString_shouldReturnFalse() {
75+
// given
76+
let url = " "
77+
78+
// when
79+
let result = sut.validate(input: url)
80+
81+
// then
82+
XCTAssertFalse(result)
83+
}
84+
85+
func test_validate_ipAddressURL_shouldReturnTrue() {
86+
// given
87+
let url = "http://192.168.0.1"
88+
89+
// when
90+
let result = sut.validate(input: url)
91+
92+
// then
93+
XCTAssertTrue(result)
94+
}
95+
96+
func test_validate_urlWithPort_shouldReturnTrue() {
97+
// given
98+
let url = "https://localhost:8080"
99+
100+
// when
101+
let result = sut.validate(input: url)
102+
103+
// then
104+
XCTAssertTrue(result)
105+
}
106+
107+
func test_validate_ftpScheme_shouldReturnTrue() {
108+
// given
109+
let url = "ftp://example.com"
110+
111+
// when
112+
let result = sut.validate(input: url)
113+
114+
// then
115+
XCTAssertTrue(result)
116+
}
117+
}
118+
119+
// MARK: Constants
120+
121+
private extension String {
122+
static let error = "URL is invalid"
123+
}

0 commit comments

Comments
 (0)