diff --git a/.gitignore b/.gitignore index 5da418c..0649719 100644 --- a/.gitignore +++ b/.gitignore @@ -88,4 +88,4 @@ fastlane/test_output # https://github.com/johnno1962/injectionforxcode iOSInjectionProject/ -./swiftpm \ No newline at end of file +.swiftpm/ \ No newline at end of file diff --git a/README.md b/README.md index 9207ee1..5e86ed1 100644 --- a/README.md +++ b/README.md @@ -302,6 +302,7 @@ struct RegistrationView: View { | `RegexValidationRule` | Pattern matching validation | `RegexValidationRule(pattern: "^\\d{3}-\\d{4}$", error: "Invalid phone format")` | | `URLValidationRule` | Validates URL format | `URLValidationRule(error: "Please enter a valid URL")` | | `CreditCardValidationRule` | Validates credit card numbers (Luhn algorithm) | `CreditCardValidationRule(error: "Invalid card number")` | +| `EmailValidationRule` | Validates email format | `EmailValidationRule(error: "Please enter a valid email")` | ## Custom Validators diff --git a/Sources/ValidatorCore/Classes/Rules/EmailValidationRule.swift b/Sources/ValidatorCore/Classes/Rules/EmailValidationRule.swift new file mode 100644 index 0000000..ff264fd --- /dev/null +++ b/Sources/ValidatorCore/Classes/Rules/EmailValidationRule.swift @@ -0,0 +1,38 @@ +// +// Validator +// Copyright © 2023 Space Code. All rights reserved. +// + +import Foundation + +/// An email validation rule. +public struct EmailValidationRule: IValidationRule { + // MARK: Types + + public typealias Input = String + + // MARK: Properties + + /// The validation error. + public let error: IValidationError + + // MARK: Initialization + + 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._%+-]+(?