-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from bdolewski/develop
First implementation and documentation
- Loading branch information
Showing
10 changed files
with
148 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,12 @@ | ||
# SwiftEmailValidator | ||
Very small e-mail validation helper written in Swift | ||
Very small e-mail validation helper written in Swift. It contains regular expresion that is compliant with RFC 2822. | ||
You can use it via Swift Package Manager or just copy file `SwiftEmailValidator.swift` into your project - done! | ||
|
||
## How do I use it in my code? | ||
Simple as this: | ||
|
||
```swift | ||
let result = EmailValidator.isValid(email: "[email protected]") | ||
``` | ||
|
||
There is only 1 static function `isValid(email:)` enclosed in _namespace_ `EmailValidator` (just a struct, really) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.DS_Store | ||
/.build | ||
/Packages | ||
/*.xcodeproj | ||
xcuserdata/ |
7 changes: 7 additions & 0 deletions
7
SwiftEmailValidator/.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
8 changes: 8 additions & 0 deletions
8
SwiftEmailValidator/.swiftpm/xcode/package.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
<plist version="1.0"> | ||
<dict> | ||
<key>IDEDidComputeMac32BitWarning</key> | ||
<true/> | ||
</dict> | ||
</plist> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// swift-tools-version:5.1 | ||
// The swift-tools-version declares the minimum version of Swift required to build this package. | ||
|
||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "SwiftEmailValidator", | ||
products: [ | ||
.library( | ||
name: "SwiftEmailValidator", | ||
targets: ["SwiftEmailValidator"]), | ||
], | ||
targets: [ | ||
// Targets are the basic building blocks of a package. A target can define a module or a test suite. | ||
// Targets can depend on other targets in this package, and on products in packages which this package depends on. | ||
.target( | ||
name: "SwiftEmailValidator", | ||
dependencies: []), | ||
.testTarget( | ||
name: "SwiftEmailValidatorTests", | ||
dependencies: ["SwiftEmailValidator"]), | ||
] | ||
) |
25 changes: 25 additions & 0 deletions
25
SwiftEmailValidator/Sources/SwiftEmailValidator/SwiftEmailValidator.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// | ||
// SwiftEmailValidator.swift | ||
// | ||
// | ||
// Created by Bartosz Dolewski on 07/09/2019. | ||
// | ||
|
||
import Foundation | ||
|
||
// MARK: - Namespace EmailValidating | ||
struct EmailValidator { | ||
|
||
/// Check if given e-mail adres is correct (compliant to RFC 2822) | ||
/// | ||
/// - Parameter email: e-mail adress to be validated | ||
/// - Returns: true if e-mail is valid, false otherwise | ||
static func isValid(email: String?) -> Bool { | ||
guard let email = email else { return false } | ||
|
||
let emailRegex = "(?:[a-z0-9!#$%\\&'*+/=?\\^_`{|}~-]+(?:\\.[a-z0-9!#$%\\&'*+/=?\\^_`{|}"+"~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\"+"x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-"+"z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5"+"]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-"+"9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21"+"-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])" | ||
|
||
let emailPredicate = NSPredicate(format: "SELF MATCHES[c] %@", emailRegex) | ||
return emailPredicate.evaluate(with: email) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import XCTest | ||
|
||
import SwiftEmailValidatorTests | ||
|
||
var tests = [XCTestCaseEntry]() | ||
tests += SwiftEmailValidatorTests.allTests() | ||
XCTMain(tests) |
40 changes: 40 additions & 0 deletions
40
SwiftEmailValidator/Tests/SwiftEmailValidatorTests/SwiftEmailValidatorTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import XCTest | ||
@testable import SwiftEmailValidator | ||
|
||
final class SwiftEmailValidatorTests: XCTestCase { | ||
func testValidEmails() { | ||
XCTAssertTrue(EmailValidator.isValid(email: "[email protected]")) | ||
XCTAssertTrue(EmailValidator.isValid(email: "[email protected]")) | ||
|
||
XCTAssertTrue(EmailValidator.isValid(email: "[email protected]")) | ||
XCTAssertTrue(EmailValidator.isValid(email: "[email protected]")) | ||
} | ||
|
||
func testInvalidEmails() { | ||
// empty e-mail | ||
XCTAssertFalse(EmailValidator.isValid(email: "")) | ||
XCTAssertFalse(EmailValidator.isValid(email: nil)) | ||
|
||
// bad apple | ||
XCTAssertFalse(EmailValidator.isValid(email: "[email protected]")) | ||
XCTAssertFalse(EmailValidator.isValid(email: "john@apple,com")) | ||
XCTAssertFalse(EmailValidator.isValid(email: "john@apple?com")) | ||
XCTAssertFalse(EmailValidator.isValid(email: "@")) | ||
XCTAssertFalse(EmailValidator.isValid(email: "john")) | ||
|
||
// bad johnnames | ||
XCTAssertFalse(EmailValidator.isValid(email: "@apple.com")) | ||
XCTAssertFalse(EmailValidator.isValid(email: "@@apple.com")) | ||
XCTAssertFalse(EmailValidator.isValid(email: "some [email protected]")) | ||
XCTAssertFalse(EmailValidator.isValid(email: "some,[email protected]")) | ||
XCTAssertFalse(EmailValidator.isValid(email: "john;@apple.com")) | ||
XCTAssertFalse(EmailValidator.isValid(email: "john<>@apple.com")) | ||
XCTAssertFalse(EmailValidator.isValid(email: "[email protected]")) | ||
XCTAssertFalse(EmailValidator.isValid(email: "1234567890123456789012345678901234567890123456789012345678901234+x@@apple.com")) | ||
} | ||
|
||
static var allTests = [ | ||
("testValidEmails", testValidEmails), | ||
("testInvalidEmails", testInvalidEmails) | ||
] | ||
} |
9 changes: 9 additions & 0 deletions
9
SwiftEmailValidator/Tests/SwiftEmailValidatorTests/XCTestManifests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import XCTest | ||
|
||
#if !canImport(ObjectiveC) | ||
public func allTests() -> [XCTestCaseEntry] { | ||
return [ | ||
testCase(SwiftEmailValidatorTests.allTests), | ||
] | ||
} | ||
#endif |