Created
April 1, 2019 11:18
-
-
Save truizlop/8b04a7982bc59e3e3ca7adb110fa1146 to your computer and use it in GitHub Desktop.
Validation with Bow 0.4.0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Bow | |
import Foundation | |
typealias FormValidationResult<T> = ValidatedNEA<FormError, T> | |
struct Form: Equatable { | |
let firstName: String | |
let lastName: String | |
let birthday: Date | |
let documentId: String | |
let phoneNumber: String | |
let email: String | |
} | |
enum FormError: Equatable { | |
case emptyFirstName(_ firstName: String) | |
case emptyLastName(_ firstName: String) | |
case userTooYoung(_ birthday: Date) | |
case invalidDocumentId(_ documentId: String) | |
case invalidPhoneNumber(_ phoneNumber: String) | |
case invalidEmail(_ email: String) | |
} | |
class FormValidator { | |
static func validateForm(_ referenceDate: Date, _ form: Form) -> ValidatedNEA<FormError, Form> { | |
return FormValidationResult<Form>.map( | |
validateFirstName(form.firstName), | |
validateLastName(form.lastName), | |
validateBirthday(referenceDate, form.birthday), | |
validateDocumentId(form.documentId), | |
validatePhoneNumber(form.phoneNumber), | |
validateEmail(form.email)) { | |
(firstName, | |
lastName, | |
birthday, | |
documentId, | |
phone, | |
email) -> Form in | |
Form(firstName: firstName, | |
lastName: lastName, | |
birthday: birthday, | |
documentId: documentId, | |
phoneNumber: phone, | |
email: email) | |
}^ | |
} | |
static func validateFirstName(_ firstName: String) -> FormValidationResult<String> { | |
if !firstName.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty { | |
return Validated.valid(firstName) | |
} else { | |
return Validated.invalid(FormError.emptyFirstName(firstName)).toValidatedNEA() | |
} | |
} | |
static func validateLastName(_ lastName: String) -> FormValidationResult<String> { | |
if !lastName.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty { | |
return Validated.valid(lastName) | |
} else { | |
return Validated.invalid(FormError.emptyLastName(lastName)).toValidatedNEA() | |
} | |
} | |
static func validateBirthday(_ referenceDate: Date, | |
_ birthday: Date) -> FormValidationResult<Date> { | |
if Calendar.current.date(byAdding: .year, value: 18, to: birthday)! < referenceDate { | |
return Validated.valid(birthday) | |
} else { | |
return Validated.invalid(FormError.userTooYoung(birthday)).toValidatedNEA() | |
} | |
} | |
static func validateDocumentId(_ documentId: String) -> FormValidationResult<String> { | |
if documentRegEx.firstMatch(in: documentId, options: [], | |
range: NSRange(location: 0, | |
length: documentId.utf16.count)) != nil { | |
return Validated.valid(documentId) | |
} else { | |
return Validated.invalid(FormError.invalidDocumentId(documentId)).toValidatedNEA() | |
} | |
} | |
static func validatePhoneNumber(_ phoneNumber: String) -> FormValidationResult<String> { | |
if phoneRegEx.firstMatch(in: phoneNumber, options: [], | |
range: NSRange(location: 0, | |
length: phoneNumber.utf16.count)) != nil { | |
return Validated.valid(phoneNumber) | |
} else{ | |
return Validated.invalid(FormError.invalidPhoneNumber(phoneNumber)).toValidatedNEA() | |
} | |
} | |
static func validateEmail(_ email: String) -> FormValidationResult<String> { | |
if email.trimmingCharacters(in: .whitespacesAndNewlines).contains("@") { | |
return Validated.valid(email) | |
} else { | |
return Validated.invalid(FormError.invalidEmail(email)).toValidatedNEA() | |
} | |
} | |
static let documentRegEx = try! NSRegularExpression(pattern: "\\d{8}[a-zA-Z]{1}") | |
static let phoneRegEx = try! NSRegularExpression(pattern: "\\d{9}") | |
} | |
let manyInvalid = Form(firstName: "", | |
lastName: " ", | |
birthday: Date(), | |
documentId: "48632500", | |
phoneNumber: "6799", | |
email: "pedro") | |
let oneInvalid = Form(firstName: "Pedro", | |
lastName: "Gómez", | |
birthday: Date.init(timeIntervalSince1970: 0), | |
documentId: "48632500C", | |
phoneNumber: "677673298", | |
email: "pedro") | |
FormValidator.validateForm(Date(), manyInvalid).description | |
FormValidator.validateForm(Date(), oneInvalid).description |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment