Skip to content
This repository has been archived by the owner on Nov 16, 2020. It is now read-only.

Commit

Permalink
Merge pull request #30 from cweinberger/vapor-2
Browse files Browse the repository at this point in the history
Critical updates for Swift 4.2, fix warnings
  • Loading branch information
tanner0101 authored Oct 3, 2018
2 parents e5a7e54 + 3ebdea1 commit 1fd71c5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
17 changes: 14 additions & 3 deletions Sources/Validation/Convenience/Alphanumeric.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
private let alphanumeric = "abcdefghijklmnopqrstuvwxyz0123456789"
#if swift(>=4)
private let validCharacters = alphanumeric
#else
private let validCharacters = alphanumeric.characters
#endif

/// A validator that can be used to check that a
/// given string contains only alphanumeric characters
Expand All @@ -13,10 +17,17 @@ public struct OnlyAlphanumeric: Validator {

- throws: an error if validation fails
*/


public func validate(_ input: String) throws {
let passed = !input
.lowercased()
.characters

#if swift(>=4)
let characters = input.lowercased()
#else
let characters = input.lowercased().characters
#endif

let passed = !characters
.contains { !validCharacters.contains($0) }

if !passed {
Expand Down
7 changes: 6 additions & 1 deletion Sources/Validation/Convenience/Base64.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ public struct Base64Validator: Validator {
private let pattern = "^(?:[a-z0-9\\+\\/]{4})*(?:[a-z0-9\\+\\/]{2}==|[a-z0-9\\+\\/]{3}=|[a-z0-9\\+\\/]{4})$"

public func validate(_ input: String) throws {
guard input.characters.count % 4 == 0 && input.range(of: pattern, options: [.regularExpression, .caseInsensitive]) != nil else {
#if swift(>=4)
let count = input.count
#else
let count = input.characters.count
#endif
guard count % 4 == 0 && input.range(of: pattern, options: [.regularExpression, .caseInsensitive]) != nil else {
throw error("\(input) is not a valid base64 string")
}
}
Expand Down
5 changes: 4 additions & 1 deletion Sources/Validation/Convenience/Count.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// Indicates that a particular type can be validated by count or length
public protocol Countable: Validatable {
// The type that will be used to evaluate the count
associatedtype CountType: Comparable, Equatable
associatedtype CountType: Comparable

// The count of the object
var count: CountType { get }
Expand Down Expand Up @@ -85,9 +85,12 @@ extension Float: Countable {}
extension Double: Countable {}

extension String: Countable {
#if swift(>=4)
#else
public var count: Int {
return characters.count
}
#endif
}

#if swift(>=4)
Expand Down

0 comments on commit 1fd71c5

Please sign in to comment.