File tree Expand file tree Collapse file tree 3 files changed +91
-0
lines changed
Sources/ValidatorCore/Classes/Rules
Tests/ValidatorCoreTests/UnitTests/Rules Expand file tree Collapse file tree 3 files changed +91
-0
lines changed Original file line number Diff line number Diff line change @@ -304,6 +304,7 @@ struct RegistrationView: View {
304304| ` CreditCardValidationRule ` | Validates credit card numbers (Luhn algorithm) | ` CreditCardValidationRule(error: "Invalid card number") ` |
305305| ` EmailValidationRule ` | Validates email format | ` EmailValidationRule(error: "Please enter a valid email") ` |
306306| ` CharactersValidationRule ` | Validates that a string contains only characters from the allowed CharacterSet | ` CharactersValidationRule(characterSet: .letters, error: "Invalid characters") ` |
307+ | ` NilValidationRule ` | Validates that value is nil | ` NilValidationRule(error: "Value must be nil") `
307308
308309## Custom Validators
309310
Original file line number Diff line number Diff line change 1+ //
2+ // Validator
3+ // Copyright © 2023 Space Code. All rights reserved.
4+ //
5+
6+ import Foundation
7+
8+ /// A nil validation rule.
9+ public struct NilValidationRule < T> : IValidationRule {
10+ // MARK: Types
11+
12+ public typealias Input = T ?
13+
14+ // MARK: Properties
15+
16+ /// The validation error.
17+ public let error : IValidationError
18+
19+ // MARK: Initialization
20+
21+ public init ( error: IValidationError ) {
22+ self . error = error
23+ }
24+
25+ // MARK: IValidationRule
26+
27+ public func validate( input: T ? ) -> Bool {
28+ input == nil
29+ }
30+ }
Original file line number Diff line number Diff line change 1+ //
2+ // Validator
3+ // Copyright © 2025 Space Code. All rights reserved.
4+ //
5+
6+ @testable import ValidatorCore
7+ import XCTest
8+
9+ // MARK: - NilValidationRuleTests
10+
11+ final class NilValidationRuleTests : XCTestCase {
12+ // MARK: - Properties
13+
14+ private var sut : NilValidationRule < String > !
15+
16+ // MARK: - Setup
17+
18+ override func setUp( ) {
19+ super. setUp ( )
20+ sut = NilValidationRule < String > ( error: String . error)
21+ }
22+
23+ override func tearDown( ) {
24+ sut = nil
25+ super. tearDown ( )
26+ }
27+
28+ // MARK: - Validation Tests
29+
30+ func testValidateWithNilInput_ReturnsTrue( ) {
31+ // Given
32+ let input : String ? = nil
33+
34+ // When
35+ let result = sut. validate ( input: input)
36+
37+ // Then
38+ XCTAssertTrue ( result)
39+ }
40+
41+ func testValidateWithNonNilInput_ReturnsFalse( ) {
42+ // Given
43+ let input : String ? = " Hello "
44+
45+ // When
46+ let result = sut. validate ( input: input)
47+
48+ // Then
49+ XCTAssertFalse ( result)
50+ }
51+
52+ func testErrorProperty_ReturnsProvidedError( ) {
53+ // Then
54+ XCTAssertEqual ( sut. error as? String , String . error)
55+ }
56+ }
57+
58+ private extension String {
59+ static let error = " Value must be nil "
60+ }
You can’t perform that action at this time.
0 commit comments