Skip to content

Commit

Permalink
Merge pull request #1 from bdolewski/develop
Browse files Browse the repository at this point in the history
First implementation and documentation
  • Loading branch information
Bartosz Dolewski authored Sep 7, 2019
2 parents 19c9232 + 2097c7d commit e461077
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 1 deletion.
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,19 @@ Carthage/Build
# For more information about the recommended setup visit:
# https://docs.fastlane.tools/best-practices/source-control/#source-control

# Created by https://www.gitignore.io/api/swiftpackagemanager
# Edit at https://www.gitignore.io/?templates=swiftpackagemanager

### SwiftPackageManager ###
Packages
.build/
xcuserdata
DerivedData/
*.xcodeproj


# End of https://www.gitignore.io/api/swiftpackagemanager

fastlane/report.xml
fastlane/Preview.html
fastlane/screenshots/**/*.png
Expand Down
12 changes: 11 additions & 1 deletion README.md
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)
5 changes: 5 additions & 0 deletions SwiftEmailValidator/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.DS_Store
/.build
/Packages
/*.xcodeproj
xcuserdata/

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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>
23 changes: 23 additions & 0 deletions SwiftEmailValidator/Package.swift
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"]),
]
)
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)
}
}
7 changes: 7 additions & 0 deletions SwiftEmailValidator/Tests/LinuxMain.swift
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)
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)
]
}
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

0 comments on commit e461077

Please sign in to comment.