Skip to content

Commit ba930ee

Browse files
authored
feat: add positive number validation rule (#82)
1 parent 78cc5d9 commit ba930ee

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ struct RegistrationView: View {
309309
| `EmailValidationRule` | Validates email format | `EmailValidationRule(error: "Please enter a valid email")` |
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")`
312+
| `PositiveNumberValidationRule` | Validates that value is positive | `PositiveNumberValidationRule(error: "Value must be positive")`
312313

313314
## Custom Validators
314315

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//
2+
// Validator
3+
// Copyright © 2025 Space Code. All rights reserved.
4+
//
5+
6+
/// Validates that a number is positive.
7+
///
8+
/// # Example:
9+
/// ```swift
10+
/// let rule = PositiveNumberValidationRule(error: "Value must be positive")
11+
/// rule.validate(input: -1) // false
12+
/// rule.validate(input: 10) // true
13+
/// ```
14+
public struct PositiveNumberValidationRule: IValidationRule {
15+
// MARK: Types
16+
17+
public typealias Input = Double
18+
19+
// MARK: Properties
20+
21+
/// The validation error.
22+
public let error: IValidationError
23+
24+
// MARK: Initialization
25+
26+
public init(error: IValidationError) {
27+
self.error = error
28+
}
29+
30+
// MARK: IValidationRule
31+
32+
public func validate(input: Double) -> Bool {
33+
input > 0
34+
}
35+
}

Sources/ValidatorCore/Validator.docc/Overview.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ ValidatorCore contains all core validation rules, utilities, and mechanisms for
3131
- ``RegexValidationRule``
3232
- ``SuffixValidationRule``
3333
- ``URLValidationRule``
34+
- ``NilValidationRule``
35+
- ``PositiveNumberValidationRule``
3436

3537
### Articles
3638

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//
2+
// Validator
3+
// Copyright © 2025 Space Code. All rights reserved.
4+
//
5+
6+
import ValidatorCore
7+
import XCTest
8+
9+
// MARK: - PositiveNumberValidationRuleTests
10+
11+
final class PositiveNumberValidationRuleTests: XCTestCase {
12+
// MARK: Properties
13+
14+
private var sut: PositiveNumberValidationRule!
15+
16+
// MARK: XCTestCase
17+
18+
override func setUp() {
19+
super.setUp()
20+
sut = PositiveNumberValidationRule(error: String.error)
21+
}
22+
23+
override func tearDown() {
24+
sut = nil
25+
super.tearDown()
26+
}
27+
28+
// MARK: Tests
29+
30+
func test_thatPositiveNumberValidationRuleSetsProperties() {
31+
// then
32+
XCTAssertEqual(sut.error.message, .error)
33+
}
34+
35+
func test_thatPositiveNumberValidationRuleValidatesInput_whenInputIsCorrectValue() {
36+
// when
37+
let result = sut.validate(input: 10)
38+
39+
// then
40+
XCTAssertTrue(result)
41+
}
42+
43+
func test_thatPositiveNumberValidationRuleValidatesInput_whenInputIsIncorrectValue() {
44+
// when
45+
let result = sut.validate(input: -1)
46+
47+
// then
48+
XCTAssertFalse(result)
49+
}
50+
}
51+
52+
// MARK: - Constants
53+
54+
private extension String {
55+
static let error = "Value must be positive"
56+
}

0 commit comments

Comments
 (0)