diff --git a/README.md b/README.md index 4b573be..e77e456 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/Sources/ValidatorCore/Classes/Rules/URLValidationRule.swift b/Sources/ValidatorCore/Classes/Rules/URLValidationRule.swift index f44ebf6..e151840 100644 --- a/Sources/ValidatorCore/Classes/Rules/URLValidationRule.swift +++ b/Sources/ValidatorCore/Classes/Rules/URLValidationRule.swift @@ -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 diff --git a/Sources/ValidatorCore/Classes/Rules/UUIDValidationRule.swift b/Sources/ValidatorCore/Classes/Rules/UUIDValidationRule.swift new file mode 100644 index 0000000..ba0aa15 --- /dev/null +++ b/Sources/ValidatorCore/Classes/Rules/UUIDValidationRule.swift @@ -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 + } +} diff --git a/Sources/ValidatorCore/Validator.docc/Overview.md b/Sources/ValidatorCore/Validator.docc/Overview.md index 5e09009..88548e6 100644 --- a/Sources/ValidatorCore/Validator.docc/Overview.md +++ b/Sources/ValidatorCore/Validator.docc/Overview.md @@ -41,6 +41,7 @@ ValidatorCore contains all core validation rules, utilities, and mechanisms for - ``IPAddressValidationRule`` - ``PostalCodeValidationRule`` - ``Base64ValidationRule`` +- ``UUIDValidationRule`` ### Articles diff --git a/Tests/ValidatorCoreTests/UnitTests/Rules/UUIDValidationRuleTests.swift b/Tests/ValidatorCoreTests/UnitTests/Rules/UUIDValidationRuleTests.swift new file mode 100644 index 0000000..95c7edb --- /dev/null +++ b/Tests/ValidatorCoreTests/UnitTests/Rules/UUIDValidationRuleTests.swift @@ -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" +}