Add e-mail validator
This commit is contained in:
parent
d10adc321e
commit
9b8f126d36
|
@ -0,0 +1,20 @@
|
|||
//
|
||||
// EmailValidator.swift
|
||||
// SwiftKit
|
||||
//
|
||||
// Created by Daniel Saidi on 2020-06-09.
|
||||
// Copyright © 2020 Daniel Saidi. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
public class EmailValidator: Validator {
|
||||
|
||||
public init() {}
|
||||
|
||||
public func validate(_ string: String) -> Bool {
|
||||
let regExp = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,} ?"
|
||||
let predicate = NSPredicate(format: "SELF MATCHES %@", regExp)
|
||||
return predicate.evaluate(with: string)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
//
|
||||
// Validator.swift
|
||||
// SwiftKit
|
||||
//
|
||||
// Created by Daniel Saidi on 2020-06-09.
|
||||
// Copyright © 2020 Daniel Saidi. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
public protocol Validator {
|
||||
|
||||
associatedtype Validation
|
||||
|
||||
func validate(_ obj: Validation) -> Bool
|
||||
}
|
|
@ -1,19 +0,0 @@
|
|||
//
|
||||
// PerformAsyncTests.swift
|
||||
// SwiftKitTests
|
||||
//
|
||||
// Created by Daniel Saidi on 2020-06-01.
|
||||
// Copyright © 2020 Daniel Saidi. All rights reserved.
|
||||
//
|
||||
|
||||
import Quick
|
||||
import Nimble
|
||||
import SwiftKit
|
||||
|
||||
class PerformTests: QuickSpec {
|
||||
|
||||
override func spec() {
|
||||
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
//
|
||||
// PerformAsyncTests.swift
|
||||
// SwiftKitTests
|
||||
//
|
||||
// Created by Daniel Saidi on 2020-06-09.
|
||||
// Copyright © 2020 Daniel Saidi. All rights reserved.
|
||||
//
|
||||
|
||||
import Quick
|
||||
import Nimble
|
||||
import SwiftKit
|
||||
|
||||
class EmailValidatorTests: QuickSpec {
|
||||
|
||||
override func spec() {
|
||||
|
||||
describe("email validator") {
|
||||
|
||||
let validate = EmailValidator().validate
|
||||
|
||||
context("when validating validating valid addresses") {
|
||||
|
||||
it("validates valid email addresses") {
|
||||
expect(validate("foobar@baz.com")).to(beTrue())
|
||||
expect(validate("foo1.bar2@baz.com")).to(beTrue())
|
||||
expect(validate("foo.bar@gmail.com")).to(beTrue())
|
||||
}
|
||||
|
||||
it("validates long top domains") {
|
||||
expect(validate("foobar@baz.co")).to(beTrue())
|
||||
expect(validate("foobar@baz.com")).to(beTrue())
|
||||
expect(validate("foo1.bar2@baz.comm")).to(beTrue())
|
||||
expect(validate("foo.bar@gmail.commmmmmmmmmmmmmmm")).to(beTrue())
|
||||
}
|
||||
}
|
||||
|
||||
context("when validating invalid addresses") {
|
||||
|
||||
it("does not validate invalid email addresses") {
|
||||
expect(validate("foobar")).to(beFalse())
|
||||
expect(validate("foo1.bar2@")).to(beFalse())
|
||||
expect(validate("foo.bar@gmail")).to(beFalse())
|
||||
}
|
||||
|
||||
it("does not validate too short top domains") {
|
||||
expect(validate("foobar@baz.c")).to(beFalse())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue