-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathEmailValidationRule.swift
More file actions
49 lines (40 loc) · 1.42 KB
/
EmailValidationRule.swift
File metadata and controls
49 lines (40 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//
// Validator
// Copyright © 2023 Space Code. All rights reserved.
//
import Foundation
/// Validates that a string is a valid email address.
/// Uses a regular expression conforming to RFC 5322 (simplified).
///
/// # Example:
/// ```swift
/// let rule = EmailValidationRule(error: "Invalid email")
/// rule.validate(input: "user@example.com") // true
/// ```
public struct EmailValidationRule: IValidationRule {
// MARK: Types
public typealias Input = String
// MARK: Properties
/// Validation error returned if the email is invalid.
public let error: IValidationError
// MARK: Initialization
/// Initializes an email validation rule.
///
/// - Parameters:
/// - error: The validation error to return if input fails validation.
public init(error: IValidationError) {
self.error = error
}
// MARK: IValidationRule
public func validate(input: String) -> Bool {
let range = NSRange(location: .zero, length: input.count)
if let regex = try? NSRegularExpression(
// swiftlint:disable:next line_length
pattern: "^(?!\\.)(?!.*\\.\\.)[A-Z0-9a-z._%+-]+(?<!\\.)@[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?(?:\\.[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?)*\\.[A-Za-z]{2,64}$",
options: [.caseInsensitive]
) {
return regex.firstMatch(in: input, range: range) != nil
}
return false
}
}