Skip to content
Merged
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ struct RegistrationView: View {
| `IPAddressValidationRule` | Validates that a string is a valid IPv4 or IPv6 address | `IPAddressValidationRule(version: .v4, error: ValidationError("Invalid IPv4"))`
| `PostalCodeValidationRule` | Validates postal/ZIP codes for different countries | `PostalCodeValidationRule(country: .uk, error: "Invalid post code")`
| `Base64ValidationRule` | | `Base64ValidationRule(error: "The input is not valid Base64.")`
| `UUIDValidationRule` | Validates UUID format | `UUIDValidationRule(error: "Please enter a valid UUID")` |

## Custom Validators

Expand Down
17 changes: 8 additions & 9 deletions Sources/ValidatorCore/Classes/Rules/URLValidationRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@

import Foundation

// Validates that a string represents a valid URL.
//
// # Example:
// ```swift
// let rule = URLValidationRule(error: "Invalid URL")
// rule.validate(input: "https://example.com") // true
// rule.validate(input: "not_a_url") // false
// ```

/// Validates that a string represents a valid URL.
///
/// # Example:
/// ```swift
/// let rule = URLValidationRule(error: "Invalid URL")
/// rule.validate(input: "https://example.com") // true
/// rule.validate(input: "not_a_url") // false
/// ```
public struct URLValidationRule: IValidationRule {
// MARK: Types

Expand Down
40 changes: 40 additions & 0 deletions Sources/ValidatorCore/Classes/Rules/UUIDValidationRule.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// Validator
// Copyright © 2023 Space Code. All rights reserved.
//

import Foundation

/// Validates that a string represents a valid UUID.
///
/// # Example:
/// ```swift
/// let rule = UUIDValidationRule(error: "Invalid UUID")
/// rule.validate(input: "47273ec2-e638-4702-8325-dcf82ed6a95b") // true
/// rule.validate(input: "47273ec2") // false
/// ```
public struct UUIDValidationRule: IValidationRule {
// MARK: Types

public typealias Input = String

// MARK: Properties

/// The validation error returned if the input is not a valid UUID.
public let error: IValidationError

// MARK: Initialization

/// Initializes a URL validation rule.
///
/// - Parameter error: The validation error returned if input fails validation.
public init(error: IValidationError) {
self.error = error
}

// MARK: IValidationRule

public func validate(input: String) -> Bool {
UUID(uuidString: input) != nil
}
}
1 change: 1 addition & 0 deletions Sources/ValidatorCore/Validator.docc/Overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ ValidatorCore contains all core validation rules, utilities, and mechanisms for
- ``IPAddressValidationRule``
- ``PostalCodeValidationRule``
- ``Base64ValidationRule``
- ``UUIDValidationRule``

### Articles

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
//
// Validator
// Copyright © 2026 Space Code. All rights reserved.
//

import ValidatorCore
import XCTest

// MARK: - UUIDValidationRuleTests

final class UUIDValidationRuleTests: XCTestCase {
// MARK: - Properties

private var sut: UUIDValidationRule!

// MARK: - Setup

override func setUp() {
super.setUp()
sut = UUIDValidationRule(error: String.error)
}

override func tearDown() {
sut = nil
super.tearDown()
}

// MARK: Tests

func test_validate_validUUID_shouldReturnTrue() {
// given
let uuid = "550e8400-e29b-41d4-a716-446655440000"

// when
let result = sut.validate(input: uuid)

// then
XCTAssertTrue(result)
}

func test_validate_emptyString_shouldReturnFalse() {
// given
let uuid = ""

// when
let result = sut.validate(input: uuid)

// then
XCTAssertFalse(result)
}

func test_validate_whitespaceString_shouldReturnFalse() {
// given
let uuid = " "

// when
let result = sut.validate(input: uuid)

// then
XCTAssertFalse(result)
}

func test_validate_invalidUUID_shouldReturnFalse() {
// given
let uuid = "not-a-uuid"

// when
let result = sut.validate(input: uuid)

// then
XCTAssertFalse(result)
}
}

// MARK: Constants

private extension String {
static let error = "UUID is invalid"
}
Loading