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

Commit c8dc97f

Browse files
committed
Ensure whole string is valid email
1 parent 1fd71c5 commit c8dc97f

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

Sources/Validation/Convenience/Email.swift

+5-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@ import Foundation
33
public struct EmailValidator: Validator {
44

55
public init() {}
6-
private let pattern = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}"
6+
private let pattern = "[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,64}"
77

88

99
public func validate(_ input: String) throws {
10-
guard input.range(of: pattern, options: [.regularExpression, .caseInsensitive]) != nil else {
10+
guard
11+
let range = input.range(of: pattern, options: [.regularExpression, .caseInsensitive])//,
12+
range.lowerBound == input.startIndex, range.upperBound == input.endIndex
13+
else {
1114
throw error("\(input) is not a valid email address")
1215
}
1316
}

Tests/ValidationTests/ValidationTests.swift

+3-1
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,11 @@ class ValidationTests: XCTestCase {
8484
XCTAssertTrue("[email protected]".passes(EmailValidator()))
8585
XCTAssertTrue("[email protected]".passes(EmailValidator()))
8686
XCTAssertTrue("[email protected]".passes(EmailValidator()))
87-
XCTAssertTrue("foo!-bar![email protected]".passes(EmailValidator()))
87+
XCTAssertTrue("[email protected]".passes(EmailValidator()))
8888
XCTAssertFalse("f@b.".passes(EmailValidator()))
8989
XCTAssertFalse("æøå@gmail.com".passes(EmailValidator()))
90+
XCTAssertFalse(">[email protected]".passes(EmailValidator()))
91+
XCTAssertFalse("[email protected]<".passes(EmailValidator()))
9092
}
9193

9294
func testValidHexadecimal() {

0 commit comments

Comments
 (0)