gafilianog / Teddit

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

User validation: Add validation case

gafilianog opened this issue · comments

Description:

username should not contain spaces " ". It is possible to use dash or underscore to separate between words like john-doe or john_doe.

Task

  • Add validation case in
    func validateUsername() throws -> String {
    let username = usernameField.text!
    if username.isEmpty {
    throw ValidationError("Username cannot be empty.")
    }
    if username.count < 3 || username.count > 20 {
    throw ValidationError("Username must be 4-20 characters.")
    }
    for char in username {
    if char == "_" {
    continue
    }
    if !char.isLetter && !char.isNumber {
    throw ValidationError("Username must not contain special characters.")
    }
    }
    var entity: User? = nil
    do {
    entity = try userRepo!.findByUsername(username: username)
    } catch {
    print("Cannot fetch from Core Data: \(error)")
    }
    if entity != nil {
    throw ValidationError("Username is already taken.")
    }
    return username
    }