enum ValidationError: Error {
case tooShort
case tooLong
case containsInvalidCharacters
}
func validate(username: String) throws -> String {
if username.count < 3 {
throw ValidationError.tooShort
}
if username.count > 20 {
throw ValidationError.tooLong
}
guard username.allSatisfy({ $0.isLetter || $0.isNumber }) else {
throw ValidationError.containsInvalidCharacters
}
return username
}