Skip to content

Commit 53946cd

Browse files
authored
Make type constraintes less strict (#5)
* Make type constraintes less strict
1 parent 8883541 commit 53946cd

File tree

6 files changed

+32
-36
lines changed

6 files changed

+32
-36
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
11
import Combine
22

33
/// Useful extension for Published wrapped values
4-
extension Published.Publisher where Value == String {
4+
extension Publisher where Output == String, Failure == Never {
55
public func validateNonEmpty(
66
error message: String,
77
tableName: String? = nil
88
) -> ValidationPublisher {
9-
NotEmptyValidator(for: self, error: message, tableName: tableName)
9+
NotEmptyValidator(for: AnyPublisher(self), error: message, tableName: tableName)
1010
}
1111

1212
public func validateWithRegex<R: RegexProtocol>(
1313
regex pattern: R,
1414
error message: String,
1515
tableName: String? = nil
1616
) -> ValidationPublisher {
17-
RegexValidator(for: self, regex: pattern, error: message, tableName: tableName)
17+
RegexValidator(for: AnyPublisher(self), regex: pattern, error: message, tableName: tableName)
1818
}
1919

2020
public func validateOneOfRegex<R: RegexProtocol>(
2121
regexs patterns: [R],
2222
error message: String,
2323
tableName: String? = nil
24-
) -> RichValidationPublisher<R> {
25-
OneOfRegexValidator(for: self, regexs: patterns, error: message, tableName: tableName)
24+
) -> ValidationPublisherOf<R> {
25+
OneOfRegexValidator(for: AnyPublisher(self), regexs: patterns, error: message, tableName: tableName)
2626
}
2727

2828
public func validateWithMultiRegex<R: RegexProtocol>(
2929
regexs patterns: [R],
3030
errors messages: [String],
3131
tableName: String? = nil
3232
) -> ValidationPublisher {
33-
MultiRegexValidator(for: self, regexs: patterns, errors: messages, tableName: tableName)
33+
MultiRegexValidator(for: AnyPublisher(self), regexs: patterns, errors: messages, tableName: tableName)
3434
}
3535
}
3636

@@ -39,7 +39,7 @@ extension Published.Publisher where Value == Bool {
3939
error message: String,
4040
tableName: String? = nil
4141
) -> ValidationPublisher {
42-
ToggleValidator(for: self, error: message, tableName: tableName)
42+
ToggleValidator(for: AnyPublisher(self), error: message, tableName: tableName)
4343
}
4444
}
4545

Sources/CombineValidate/Publishers/Publishers+Validators.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Foundation
22
import Combine
33

44
public func NotEmptyValidator(
5-
for publisher: Published<String>.Publisher,
5+
for publisher: AnyPublisher<String, Never>,
66
error message: String,
77
tableName: String? = nil
88
) -> ValidationPublisher {
@@ -15,7 +15,7 @@ public func NotEmptyValidator(
1515
}
1616

1717
public func RegexValidator<Pattern>(
18-
for publisher: Published<String>.Publisher,
18+
for publisher: AnyPublisher<String, Never>,
1919
regex pattern: Pattern,
2020
error message: String,
2121
tableName: String? = nil
@@ -29,11 +29,11 @@ public func RegexValidator<Pattern>(
2929
}
3030

3131
public func OneOfRegexValidator<Pattern>(
32-
for publisher: Published<String>.Publisher,
32+
for publisher: AnyPublisher<String, Never>,
3333
regexs patterns: [Pattern],
3434
error message: String,
3535
tableName: String? = nil
36-
) -> RichValidationPublisher<Pattern> where Pattern: RegexProtocol {
36+
) -> ValidationPublisherOf<Pattern> where Pattern: RegexProtocol {
3737
publisher
3838
.dropFirst()
3939
.debounce(for: .seconds(0.25), scheduler: RunLoop.main)
@@ -49,7 +49,7 @@ public func OneOfRegexValidator<Pattern>(
4949
}
5050

5151
public func MultiRegexValidator<Pattern>(
52-
for publisher: Published<String>.Publisher,
52+
for publisher: AnyPublisher<String, Never>,
5353
regexs patterns: [Pattern],
5454
errors messages: [String],
5555
tableName: String? = nil
@@ -77,7 +77,7 @@ public func MultiRegexValidator<Pattern>(
7777
}
7878

7979
public func ToggleValidator(
80-
for publisher: Published<Bool>.Publisher,
80+
for publisher: AnyPublisher<Bool, Never>,
8181
error message: String,
8282
tableName: String? = nil
8383
) -> ValidationPublisher {

Sources/CombineValidate/Publishers/ValidationPublisher.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ public typealias ValidationPublisher = AnyPublisher<Validated<Void>, Never>
1313
///
1414
/// Use when you want to push through pipeline particular success case conforming ``RegexProtocol``
1515
/// Uses with ``OneOfRegexValidator(for:regexs:error:tableName:)``
16-
public typealias RichValidationPublisher<T> = AnyPublisher<Validated<T>, Never>
16+
public typealias ValidationPublisherOf<T> = AnyPublisher<Validated<T>, Never>

Sources/CombineValidate/Views/ValidationWrapper.swift

+14-9
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public struct ValidationWrapper<ValidatedView: View, ValidationPayload>: View {
77
@State private var validated: Validated<ValidationPayload> = .untouched
88

99
private let content: ValidatedView
10-
private let publisher: RichValidationPublisher<ValidationPayload>
10+
private let publisher: ValidationPublisherOf<ValidationPayload>
1111
private let configuration: Self.Configuration
1212

1313
public init(
@@ -22,7 +22,7 @@ public struct ValidationWrapper<ValidatedView: View, ValidationPayload>: View {
2222

2323
public init(
2424
_ content: ValidatedView,
25-
_ publisher: RichValidationPublisher<ValidationPayload>,
25+
_ publisher: ValidationPublisherOf<ValidationPayload>,
2626
configuration: Self.Configuration
2727
) where ValidationPayload: RegexProtocol {
2828
self.content = content
@@ -80,15 +80,20 @@ public struct ValidationWrapper<ValidatedView: View, ValidationPayload>: View {
8080
}
8181

8282
struct ValidationWrapper_Previews: PreviewProvider {
83+
struct Person {
84+
var email: String
85+
}
86+
8387
class ViewModel: ObservableObject {
84-
@Published var email = ""
88+
@Published var person = Person(email: "")
8589

8690
public lazy var emailValidator: ValidationPublisher = {
87-
$email.validateWithRegex(
88-
regex: RegularPattern.email,
89-
error: "Not email",
90-
tableName: nil
91-
)
91+
$person.map(\.email)
92+
.validateWithRegex(
93+
regex: RegularPattern.email,
94+
error: "Not email",
95+
tableName: nil
96+
)
9297
}()
9398
}
9499

@@ -98,7 +103,7 @@ struct ValidationWrapper_Previews: PreviewProvider {
98103
var body: some View {
99104
NavigationView {
100105
List {
101-
TextField("Email", text: $viewModel.email)
106+
TextField("Email", text: $viewModel.person.email)
102107
.validate(for: viewModel.emailValidator)
103108
}
104109
}

Sources/CombineValidate/Views/View+Validate.swift

+3-12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Combine
22
import SwiftUI
33

4-
extension TextField {
4+
extension View {
55
public func validate(
66
for publisher: ValidationPublisher,
77
configuration: ValidationWrapper<Self, Void>.Configuration = .default
@@ -10,23 +10,14 @@ extension TextField {
1010
}
1111

1212
public func validate<T: RegexProtocol>(
13-
for publisher: RichValidationPublisher<T>,
13+
for publisher: ValidationPublisherOf<T>,
1414
configuration: ValidationWrapper<Self, T>.Configuration = .default
1515
) -> some View {
1616
ValidationWrapper(self, publisher, configuration: configuration)
1717
}
1818
}
1919

20-
extension SecureField {
21-
public func validate(
22-
for publisher: ValidationPublisher,
23-
configuration: ValidationWrapper<Self, Void>.Configuration = .default
24-
) -> some View {
25-
ValidationWrapper(self, publisher, configuration: configuration)
26-
}
27-
}
28-
29-
extension Toggle {
20+
extension View {
3021
public func validate(
3122
for publisher: ValidationPublisher
3223
) -> some View {

Tests/CombineValidateTests/OneOfRegexValidatorTests.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ final class OneOfRegexValidatorTests: XCTestCase {
1414
@Published var socialProfileUrl = ""
1515
@Published var validationResult: Validated<SocialLinkPattern> = .untouched
1616

17-
public lazy var socialProfileValidator: RichValidationPublisher<SocialLinkPattern> = {
17+
public lazy var socialProfileValidator: ValidationPublisherOf<SocialLinkPattern> = {
1818
$socialProfileUrl.validateOneOfRegex(
1919
regexs: [.facebook, .linkedIn, .instagram],
2020
error: "Type one of social profile link (insta, facebook, linkedIn)"

0 commit comments

Comments
 (0)