Skip to content

Commit 1587361

Browse files
committed
feat: add uuid validation rule
1 parent 42d044c commit 1587361

File tree

5 files changed

+129
-9
lines changed

5 files changed

+129
-9
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ struct RegistrationView: View {
318318
| `IPAddressValidationRule` | Validates that a string is a valid IPv4 or IPv6 address | `IPAddressValidationRule(version: .v4, error: ValidationError("Invalid IPv4"))`
319319
| `PostalCodeValidationRule` | Validates postal/ZIP codes for different countries | `PostalCodeValidationRule(country: .uk, error: "Invalid post code")`
320320
| `Base64ValidationRule` | | `Base64ValidationRule(error: "The input is not valid Base64.")`
321+
| `UUIDValidationRule` | Validates UUID format | `UUIDValidationRule(error: "Please enter a valid UUID")` |
321322

322323
## Custom Validators
323324

Sources/ValidatorCore/Classes/Rules/URLValidationRule.swift

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@
55

66
import Foundation
77

8-
// Validates that a string represents a valid URL.
9-
//
10-
// # Example:
11-
// ```swift
12-
// let rule = URLValidationRule(error: "Invalid URL")
13-
// rule.validate(input: "https://example.com") // true
14-
// rule.validate(input: "not_a_url") // false
15-
// ```
16-
8+
/// Validates that a string represents a valid URL.
9+
///
10+
/// # Example:
11+
/// ```swift
12+
/// let rule = URLValidationRule(error: "Invalid URL")
13+
/// rule.validate(input: "https://example.com") // true
14+
/// rule.validate(input: "not_a_url") // false
15+
/// ```
1716
public struct URLValidationRule: IValidationRule {
1817
// MARK: Types
1918

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//
2+
// Validator
3+
// Copyright © 2026 Space Code. All rights reserved.
4+
//
5+
6+
import Foundation
7+
8+
/// Validates that a string represents a valid UUID.
9+
///
10+
/// # Example:
11+
/// ```swift
12+
/// let rule = UUIDValidationRule(error: "Invalid UUID")
13+
/// rule.validate(input: "47273ec2-e638-4702-8325-dcf82ed6a95b") // true
14+
/// rule.validate(input: "47273ec2") // false
15+
/// ```
16+
public struct UUIDValidationRule: IValidationRule {
17+
// MARK: Types
18+
19+
public typealias Input = String
20+
21+
// MARK: Properties
22+
23+
/// The validation error returned if the input is not a valid UUID.
24+
public let error: IValidationError
25+
26+
// MARK: Initialization
27+
28+
/// Initializes a URL validation rule.
29+
///
30+
/// - Parameter error: The validation error returned if input fails validation.
31+
public init(error: IValidationError) {
32+
self.error = error
33+
}
34+
35+
// MARK: IValidationRule
36+
37+
public func validate(input: String) -> Bool {
38+
UUID(uuidString: input) != nil
39+
}
40+
}

Sources/ValidatorCore/Validator.docc/Overview.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ ValidatorCore contains all core validation rules, utilities, and mechanisms for
4141
- ``IPAddressValidationRule``
4242
- ``PostalCodeValidationRule``
4343
- ``Base64ValidationRule``
44+
- ``UUIDValidationRule``
4445

4546
### Articles
4647

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
//
2+
// Validator
3+
// Copyright © 2026 Space Code. All rights reserved.
4+
//
5+
6+
import ValidatorCore
7+
import XCTest
8+
9+
// MARK: - UUIDValidationRuleTests
10+
11+
final class UUIDValidationRuleTests: XCTestCase {
12+
// MARK: - Properties
13+
14+
private var sut: UUIDValidationRule!
15+
16+
// MARK: - Setup
17+
18+
override func setUp() {
19+
super.setUp()
20+
sut = UUIDValidationRule(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_validUUID_shouldReturnTrue() {
31+
// given
32+
let uuid = "550e8400-e29b-41d4-a716-446655440000"
33+
34+
// when
35+
let result = sut.validate(input: uuid)
36+
37+
// then
38+
XCTAssertTrue(result)
39+
}
40+
41+
func test_validate_emptyString_shouldReturnFalse() {
42+
// given
43+
let uuid = ""
44+
45+
// when
46+
let result = sut.validate(input: uuid)
47+
48+
// then
49+
XCTAssertFalse(result)
50+
}
51+
52+
func test_validate_whitespaceString_shouldReturnFalse() {
53+
// given
54+
let uuid = " "
55+
56+
// when
57+
let result = sut.validate(input: uuid)
58+
59+
// then
60+
XCTAssertFalse(result)
61+
}
62+
63+
func test_validate_invalidUUID_shouldReturnFalse() {
64+
// given
65+
let uuid = "not-a-uuid"
66+
67+
// when
68+
let result = sut.validate(input: uuid)
69+
70+
// then
71+
XCTAssertFalse(result)
72+
}
73+
}
74+
75+
// MARK: Constants
76+
77+
private extension String {
78+
static let error = "UUID is invalid"
79+
}

0 commit comments

Comments
 (0)