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 @@ -310,6 +310,7 @@ struct RegistrationView: View {
| `CharactersValidationRule` | Validates that a string contains only characters from the allowed CharacterSet | `CharactersValidationRule(characterSet: .letters, error: "Invalid characters")` |
| `NilValidationRule` | Validates that value is nil | `NilValidationRule(error: "Value must be nil")`
| `PositiveNumberValidationRule` | Validates that value is positive | `PositiveNumberValidationRule(error: "Value must be positive")`
| `NoWhitespaceValidationRule` | Validates that a string does not contain any whitespace characters | `NoWhitespaceValidationRule(error: "Spaces are not allowed")`

## Custom Validators

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

/// Validates that a string does not contain any whitespace characters.
///
/// # Example:
/// ```swift
/// let rule = NoWhitespaceValidationRule(error: "Spaces are not allowed")
/// rule.validate(input: "lorem lorem") // false
/// rule.validate(input: "Text") // true
/// ```
public struct NoWhitespaceValidationRule: IValidationRule {
// MARK: Types

public typealias Input = String

// MARK: Properties

/// The validation error.
public let error: IValidationError

// MARK: Initialization

/// Initializes a characters validation rule.
///
/// - Parameters:
/// - error: The validation error to return if input fails validation.
public init(error: IValidationError) {
self.error = error
}

// MARK: IValidationRule

public func validate(input: String) -> Bool {
input.rangeOfCharacter(from: .whitespacesAndNewlines) == 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 @@ -33,6 +33,7 @@ ValidatorCore contains all core validation rules, utilities, and mechanisms for
- ``URLValidationRule``
- ``NilValidationRule``
- ``PositiveNumberValidationRule``
- ``NoWhitespaceValidationRuleTests``

### Articles

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

import ValidatorCore
import XCTest

// MARK: - NoWhitespaceValidationRuleTests

final class NoWhitespaceValidationRuleTests: XCTestCase {
// MARK: Properties

private var sut: NoWhitespaceValidationRule!

// MARK: XCTestCase

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

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

// MARK: Tests

func test_thatNoWhitespaceValidationRuleSetsProperties() {
// then
XCTAssertEqual(sut.error.message, .error)
}

func test_thatNoWhitespaceValidationRuleValidatesInput_whenInputIsCorrectValue() {
// when
let result = sut.validate(input: "text")

// then
XCTAssertTrue(result)
}

func test_thatNoWhitespaceValidationRuleValidatesInput_whenInputIsIncorrectValue() {
// when
let result = sut.validate(input: .error)

// then
XCTAssertFalse(result)
}

func test_thatNoWhitespaceValidationRuleFails_whenInputContainsSpace() {
// when
let result = sut.validate(input: "hello world")

// then
XCTAssertFalse(result)
}

func test_thatNoWhitespaceValidationRuleFails_whenInputContainsTab() {
// when
let result = sut.validate(input: "hello\tworld")

// then
XCTAssertFalse(result)
}

func test_thatNoWhitespaceValidationRuleFails_whenInputContainsNewline() {
// when
let result = sut.validate(input: "hello\nworld")

// then
XCTAssertFalse(result)
}

func test_thatNoWhitespaceValidationRulePasses_whenInputIsEmptyString() {
// when
let result = sut.validate(input: "")

// then
XCTAssertTrue(result)
}

func test_thatNoWhitespaceValidationRulePasses_whenInputContainsSymbolsOnly() {
// when
let result = sut.validate(input: "!@#$%^&*()")

// then
XCTAssertTrue(result)
}
}

// MARK: - Constants

private extension String {
static let error = "Spaces are not allowed"
}