Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit 55115e0
Author: Alex O <[email protected]>
Date:   Mon Nov 8 15:21:46 2021 +0300

    Add remained tests

commit fa36b8e
Author: Alex O <[email protected]>
Date:   Mon Nov 8 15:21:28 2021 +0300

    Clean up extended regexps enums

commit f77d652
Author: Alex O <[email protected]>
Date:   Mon Nov 8 15:20:54 2021 +0300

    Fix validated failure case comparison

commit 966731d
Author: Alex O <[email protected]>
Date:   Mon Nov 8 15:20:17 2021 +0300

    Fix MultiRegex right error return

commit a670f07
Author: Alex O <[email protected]>
Date:   Mon Nov 8 15:19:40 2021 +0300

    Add more regexps
  • Loading branch information
pridees committed Nov 8, 2021
1 parent b5261c7 commit 6925a06
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ public func MultiRegexValidator<Pattern>(
}
return validationResults
}
.map { $0.allSatisfy { $0.isSuccess } ? .success(.none) : $0.first! }
.map { $0.allSatisfy { $0.isSuccess }
? .success(.none)
: $0.first { $0 != .untouched && $0 != .success(.none) }!
}
.eraseToAnyPublisher()
}

Expand Down
16 changes: 14 additions & 2 deletions Sources/CombineValidate/Regex.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ public protocol RegexProtocol where Self: RawRepresentable, Self.RawValue == Reg
var pattern: RegexPattern { get }
}

public extension RegexProtocol {
var pattern: RegexPattern { self.rawValue }
}

/// Extensions with predefined popular regular expressions
///
/// Conforming to ``RegexPattern`` & ``RegexProtocol``
Expand All @@ -23,9 +27,17 @@ public enum RegularPattern: RegexPattern, RegexProtocol {
/// - 1 number
case strongPassword = #"^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[a-zA-Z]).{8,}$"#

case notEmpty = #"[\S\s]+[\S]+"#

case mustIncludeCapitalLetters = #"(?=.*[A-Z])"#

case mustIncludeSpecialSymbols = #"(?=.*[\%\!-\/\:-\@\[-\`\{-\~])"#

case mustIncludeNumbers = #"(?=.*[0-9])"#

case mustIncludeSmallLetters = #"(?=.*[a-z])"#

case wordAndDigitsOnly = #"^(\d|\w|\S){1,}$"#

case anyInOneLine = #"^.+$"#

public var pattern: RegexPattern { self.rawValue }
}
4 changes: 3 additions & 1 deletion Sources/CombineValidate/Validated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ public enum Validated<Payload> : Equatable {
switch (lhs, rhs) {
case (.untouched, .untouched): return true
case (.success, .success): return true
case (.failure, .failure): return true
case (.failure(let reasonLhs, let tableNameLhs), .failure(let reasonRhs, let tableNameRhs)):
guard reasonLhs == reasonRhs && tableNameLhs == tableNameRhs else { return false }
return true
default: return false
}
}
Expand Down
4 changes: 0 additions & 4 deletions Sources/CombineValidateExtended/CombineValidateExtended.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ public enum CreditCardPattern: RegexPattern, RegexProtocol {

/// Allows inserting expiry date as MM/YYYY or MM-YYYY format
case cardExpireDate = #"^(0[1-9]|1[0-2])(\/|-)([0-9]{4})$"#

public var pattern: RegexPattern { self.rawValue }
}


Expand All @@ -65,6 +63,4 @@ public enum URLPatterns: RegexPattern, RegexProtocol {
/// Youtube link looks like
/// `https://www.youtube.com/watch?v=dQw4w9WgXcQ`
case youtubeURL = #"/(?:https?://)?(?:(?:(?:www\.?)?youtube\.com(?:/(?:(?:watch\?.*?(v=[^&\s]+).*)|(?:v(/.*))|(channel/.+)|(?:user/(.+))|(?:results\?(search_query=.+))))?)|(?:youtu\.be(/.*)?))/g"#

public var pattern: RegexPattern { self.rawValue }
}
64 changes: 64 additions & 0 deletions Tests/CombineValidateTests/MultiRegexValidatorTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import XCTest
import Combine
@testable import CombineValidate

class MultiRegexValidatorTests: XCTestCase {
class ViewModel: ObservableObject {

@Published var email = ""
@Published var validationResult: Validated<Void> = .untouched

public lazy var emailValidator: ValidationPublisher = {
$email.validateWithMultiRegex(
regexs: [RegularPattern.mustIncludeNumbers, RegularPattern.mustIncludeSpecialSymbols, RegularPattern.mustIncludeCapitalLetters],
errors: ["Should be one number at least", "Should be one special symbol at least", "Should be one capital letter at least"],
tableName: nil
)
}()

private var subscription = Set<AnyCancellable>()

init() {
emailValidator
.assign(to: \.validationResult, on: self)
.store(in: &subscription)
}
}

let viewModel = ViewModel()

func testIgnoreFirstValue() {
XCTAssertEqual(viewModel.validationResult, .untouched)
}

func testFullyInvalidValue() {
viewModel.email = ""

XCTAssertEqual(
viewModel.validationResult,
.failure(reason: "Should be one number at least", tableName: nil)
)
}

func testValueWithOneNumber() {
viewModel.email = "1"

XCTAssertEqual(viewModel.validationResult, .failure(reason: "Should be one special symbol at least", tableName: nil))
}

func testValueWithOneNumberAndSpecialSymbol() {
viewModel.email = "1%"

XCTAssertEqual(
viewModel.validationResult,
.failure(reason: "Should be one capital letter at least", tableName: nil)
)
}

func testValueWithOneNumberAndSpecialSymbolAndCapitalLetter() {
viewModel.email = "1%A"

XCTAssertEqual(viewModel.validationResult, .success(.none))
}

}
4 changes: 0 additions & 4 deletions Tests/CombineValidateTests/OneOfRegexValidatorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ final class OneOfRegexValidatorTests: XCTestCase {
case facebook = #"(?:https?:\/\/)?(?:www\.)?facebook\.com\/(?:(?:\w)*#!\/)?(?:pages\/)?(?:[\w\-]*\/)*?(\/)?([\w\-\.]{5,})"#
case linkedIn = #"^(http(s)?:\/\/)?([\w]+\.)?linkedin\.com\/(pub|in|profile)"#
case instagram = #"(?:(?:http|https):\/\/)?(?:www.)?(?:instagram.com|instagr.am|instagr.com)\/(\w+)"#

var pattern: RegexPattern {
self.rawValue
}
}

class ViewModel: ObservableObject {
Expand Down
41 changes: 41 additions & 0 deletions Tests/CombineValidateTests/RegexValidatorTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import XCTest
import Combine
@testable import CombineValidate

class RegexValidatorTests: XCTestCase {
class ViewModel: ObservableObject {

@Published var email = ""
@Published var validationResult: Validated<Void> = .untouched

public lazy var emailValidator: ValidationPublisher = {
$email.validateWithRegex(regex: RegularPattern.email, error: "Should be email", tableName: nil)
}()

private var subscription = Set<AnyCancellable>()

init() {
emailValidator
.assign(to: \.validationResult, on: self)
.store(in: &subscription)
}
}

let viewModel = ViewModel()

func testIgnoreFirstValue() {
XCTAssertEqual(viewModel.validationResult, .untouched)
}

func testValidEmailValue() {
viewModel.email = "[email protected]"

XCTAssertEqual(viewModel.validationResult, .success(.none))
}

func testInvalidEmailValue() {
viewModel.email = "someemailgmail.com"

XCTAssertEqual(viewModel.validationResult, .failure(reason: "Should be email", tableName: nil))
}
}

0 comments on commit 6925a06

Please sign in to comment.