Skip to content

Commit 4fd7933

Browse files
authored
feat: add no-whitespace validation rule (#83)
1 parent 755d63e commit 4fd7933

File tree

4 files changed

+137
-0
lines changed

4 files changed

+137
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ struct RegistrationView: View {
310310
| `CharactersValidationRule` | Validates that a string contains only characters from the allowed CharacterSet | `CharactersValidationRule(characterSet: .letters, error: "Invalid characters")` |
311311
| `NilValidationRule` | Validates that value is nil | `NilValidationRule(error: "Value must be nil")`
312312
| `PositiveNumberValidationRule` | Validates that value is positive | `PositiveNumberValidationRule(error: "Value must be positive")`
313+
| `NoWhitespaceValidationRule` | Validates that a string does not contain any whitespace characters | `NoWhitespaceValidationRule(error: "Spaces are not allowed")`
313314

314315
## Custom Validators
315316

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//
2+
// Validator
3+
// Copyright © 2025 Space Code. All rights reserved.
4+
//
5+
6+
/// Validates that a string does not contain any whitespace characters.
7+
///
8+
/// # Example:
9+
/// ```swift
10+
/// let rule = NoWhitespaceValidationRule(error: "Spaces are not allowed")
11+
/// rule.validate(input: "lorem lorem") // false
12+
/// rule.validate(input: "Text") // true
13+
/// ```
14+
public struct NoWhitespaceValidationRule: IValidationRule {
15+
// MARK: Types
16+
17+
public typealias Input = String
18+
19+
// MARK: Properties
20+
21+
/// The validation error.
22+
public let error: IValidationError
23+
24+
// MARK: Initialization
25+
26+
/// Initializes a characters validation rule.
27+
///
28+
/// - Parameters:
29+
/// - error: The validation error to return if input fails validation.
30+
public init(error: IValidationError) {
31+
self.error = error
32+
}
33+
34+
// MARK: IValidationRule
35+
36+
public func validate(input: String) -> Bool {
37+
input.rangeOfCharacter(from: .whitespacesAndNewlines) == nil
38+
}
39+
}

Sources/ValidatorCore/Validator.docc/Overview.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ ValidatorCore contains all core validation rules, utilities, and mechanisms for
3333
- ``URLValidationRule``
3434
- ``NilValidationRule``
3535
- ``PositiveNumberValidationRule``
36+
- ``NoWhitespaceValidationRuleTests``
3637

3738
### Articles
3839

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
//
2+
// Validator
3+
// Copyright © 2025 Space Code. All rights reserved.
4+
//
5+
6+
import ValidatorCore
7+
import XCTest
8+
9+
// MARK: - NoWhitespaceValidationRuleTests
10+
11+
final class NoWhitespaceValidationRuleTests: XCTestCase {
12+
// MARK: Properties
13+
14+
private var sut: NoWhitespaceValidationRule!
15+
16+
// MARK: XCTestCase
17+
18+
override func setUp() {
19+
super.setUp()
20+
sut = NoWhitespaceValidationRule(error: String.error)
21+
}
22+
23+
override func tearDown() {
24+
sut = nil
25+
super.tearDown()
26+
}
27+
28+
// MARK: Tests
29+
30+
func test_thatNoWhitespaceValidationRuleSetsProperties() {
31+
// then
32+
XCTAssertEqual(sut.error.message, .error)
33+
}
34+
35+
func test_thatNoWhitespaceValidationRuleValidatesInput_whenInputIsCorrectValue() {
36+
// when
37+
let result = sut.validate(input: "text")
38+
39+
// then
40+
XCTAssertTrue(result)
41+
}
42+
43+
func test_thatNoWhitespaceValidationRuleValidatesInput_whenInputIsIncorrectValue() {
44+
// when
45+
let result = sut.validate(input: .error)
46+
47+
// then
48+
XCTAssertFalse(result)
49+
}
50+
51+
func test_thatNoWhitespaceValidationRuleFails_whenInputContainsSpace() {
52+
// when
53+
let result = sut.validate(input: "hello world")
54+
55+
// then
56+
XCTAssertFalse(result)
57+
}
58+
59+
func test_thatNoWhitespaceValidationRuleFails_whenInputContainsTab() {
60+
// when
61+
let result = sut.validate(input: "hello\tworld")
62+
63+
// then
64+
XCTAssertFalse(result)
65+
}
66+
67+
func test_thatNoWhitespaceValidationRuleFails_whenInputContainsNewline() {
68+
// when
69+
let result = sut.validate(input: "hello\nworld")
70+
71+
// then
72+
XCTAssertFalse(result)
73+
}
74+
75+
func test_thatNoWhitespaceValidationRulePasses_whenInputIsEmptyString() {
76+
// when
77+
let result = sut.validate(input: "")
78+
79+
// then
80+
XCTAssertTrue(result)
81+
}
82+
83+
func test_thatNoWhitespaceValidationRulePasses_whenInputContainsSymbolsOnly() {
84+
// when
85+
let result = sut.validate(input: "!@#$%^&*()")
86+
87+
// then
88+
XCTAssertTrue(result)
89+
}
90+
}
91+
92+
// MARK: - Constants
93+
94+
private extension String {
95+
static let error = "Spaces are not allowed"
96+
}

0 commit comments

Comments
 (0)