Skip to content

Commit a2dc270

Browse files
committed
Implement validation rules
1 parent 9b4d743 commit a2dc270

6 files changed

Lines changed: 274 additions & 0 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//
2+
// Validator
3+
// Copyright © 2023 Space Code. All rights reserved.
4+
//
5+
6+
import Foundation
7+
8+
/// A non empty validation rule.
9+
public struct NonEmptyValidationRule: IValidationRule {
10+
// MARK: Types
11+
12+
public typealias Input = String
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: String) -> Bool {
28+
!input.isEmpty
29+
}
30+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//
2+
// Validator
3+
// Copyright © 2023 Space Code. All rights reserved.
4+
//
5+
6+
import Foundation
7+
8+
/// A prefix validation rule.
9+
public struct PrefixValidationRule: IValidationRule {
10+
// MARK: Types
11+
12+
public typealias Input = String
13+
14+
// MARK: Properties
15+
16+
/// The prefix.
17+
public let prefix: Input
18+
19+
/// The validation error.
20+
public let error: IValidationError
21+
22+
// MARK: Initialization
23+
24+
public init(prefix: Input, error: IValidationError) {
25+
self.prefix = prefix
26+
self.error = error
27+
}
28+
29+
// MARK: IValidationRule
30+
31+
public func validate(input: String) -> Bool {
32+
input.hasPrefix(prefix)
33+
}
34+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//
2+
// Validator
3+
// Copyright © 2023 Space Code. All rights reserved.
4+
//
5+
6+
import Foundation
7+
8+
/// A suffix validation rule.
9+
public struct SuffixValidationRule: IValidationRule {
10+
// MARK: Types
11+
12+
public typealias Input = String
13+
14+
// MARK: Properties
15+
16+
/// The suffix.
17+
public let suffix: Input
18+
19+
/// The validation error.
20+
public let error: IValidationError
21+
22+
// MARK: Initialization
23+
24+
public init(suffix: Input, error: IValidationError) {
25+
self.suffix = suffix
26+
self.error = error
27+
}
28+
29+
// MARK: IValidationRule
30+
31+
public func validate(input: String) -> Bool {
32+
input.hasSuffix(suffix)
33+
}
34+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//
2+
// Validator
3+
// Copyright © 2023 Space Code. All rights reserved.
4+
//
5+
6+
import ValidatorCore
7+
import XCTest
8+
9+
// MARK: - NonEmptyValidationRuleTests
10+
11+
final class NonEmptyValidationRuleTests: XCTestCase {
12+
// MARK: Properties
13+
14+
private var sut: NonEmptyValidationRule!
15+
16+
// MARK: XCTestCase
17+
18+
override func setUp() {
19+
super.setUp()
20+
sut = NonEmptyValidationRule(error: String.error)
21+
}
22+
23+
override func tearDown() {
24+
sut = nil
25+
super.tearDown()
26+
}
27+
28+
// MARK: Tests
29+
30+
func test_thatNonEmptyValidationRuleSetsProperties() {
31+
// then
32+
XCTAssertEqual(sut.error.message, .error)
33+
}
34+
35+
func test_thatNonEmptyValidationRuleValidatesInput_whenInputIsCorrectValue() {
36+
// when
37+
let result = sut.validate(input: .input)
38+
39+
// then
40+
XCTAssertTrue(result)
41+
}
42+
43+
func test_thatNonEmptyValidationRuleValidatesInput_whenInputIsIncorrectValue() {
44+
// when
45+
let result = sut.validate(input: .empty)
46+
47+
// then
48+
XCTAssertFalse(result)
49+
}
50+
}
51+
52+
// MARK: - Constants
53+
54+
private extension String {
55+
static let empty = ""
56+
static let input = "lorem ipsum lorem ipsum lorem ipsum"
57+
static let error = "error"
58+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//
2+
// Validator
3+
// Copyright © 2023 Space Code. All rights reserved.
4+
//
5+
6+
import ValidatorCore
7+
import XCTest
8+
9+
// MARK: - PrefixValidationRuleTests
10+
11+
final class PrefixValidationRuleTests: XCTestCase {
12+
// MARK: Properties
13+
14+
private var sut: PrefixValidationRule!
15+
16+
// MARK: XCTestCase
17+
18+
override func setUp() {
19+
super.setUp()
20+
sut = PrefixValidationRule(prefix: .prefix, error: String.error)
21+
}
22+
23+
override func tearDown() {
24+
sut = nil
25+
super.tearDown()
26+
}
27+
28+
// MARK: Tests
29+
30+
func test_thatPrefixValidationRuleSetsProperties() {
31+
// then
32+
XCTAssertEqual(sut.prefix, .prefix)
33+
XCTAssertEqual(sut.error.message, .error)
34+
}
35+
36+
func test_thatPrefixValidationRuleValidatesInput_whenInputIsCorrectValue() {
37+
// when
38+
let result = sut.validate(input: .input)
39+
40+
// then
41+
XCTAssertTrue(result)
42+
}
43+
44+
func test_thatPrefixValidationRuleValidatesInput_whenInputIsIncorrectValue() {
45+
// when
46+
let result = sut.validate(input: .error)
47+
48+
// then
49+
XCTAssertFalse(result)
50+
}
51+
}
52+
53+
// MARK: - Constants
54+
55+
private extension String {
56+
static let prefix = "lorem"
57+
static let input = "lorem ipsum lorem ipsum lorem ipsum"
58+
static let error = "error"
59+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//
2+
// Validator
3+
// Copyright © 2023 Space Code. All rights reserved.
4+
//
5+
6+
import ValidatorCore
7+
import XCTest
8+
9+
// MARK: - SuffixValidationRuleTests
10+
11+
final class SuffixValidationRuleTests: XCTestCase {
12+
// MARK: Properties
13+
14+
private var sut: SuffixValidationRule!
15+
16+
// MARK: XCTestCase
17+
18+
override func setUp() {
19+
super.setUp()
20+
sut = SuffixValidationRule(suffix: .suffix, error: String.error)
21+
}
22+
23+
override func tearDown() {
24+
sut = nil
25+
super.tearDown()
26+
}
27+
28+
// MARK: Tests
29+
30+
func test_thatSuffixValidationRuleSetsProperties() {
31+
// then
32+
XCTAssertEqual(sut.suffix, .suffix)
33+
XCTAssertEqual(sut.error.message, .error)
34+
}
35+
36+
func test_thatSuffixValidationRuleValidatesInput_whenInputIsCorrectValue() {
37+
// when
38+
let result = sut.validate(input: .input)
39+
40+
// then
41+
XCTAssertTrue(result)
42+
}
43+
44+
func test_thatSuffixValidationRuleValidatesInput_whenInputIsIncorrectValue() {
45+
// when
46+
let result = sut.validate(input: .error)
47+
48+
// then
49+
XCTAssertFalse(result)
50+
}
51+
}
52+
53+
// MARK: - Constants
54+
55+
private extension String {
56+
static let suffix = "ipsum"
57+
static let input = "lorem ipsum lorem ipsum lorem ipsum"
58+
static let error = "error"
59+
}

0 commit comments

Comments
 (0)