UserDefaultValidationService.swift 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. //
  2. // UserDefaultValidationService.swift
  3. // JChat
  4. //
  5. // Created by deng on 2017/8/11.
  6. // Copyright © 2017年 HXHG. All rights reserved.
  7. //
  8. import UIKit
  9. final class UserDefaultValidationService: UserValidationService {
  10. static let sharedValidationService = UserDefaultValidationService()
  11. private init () {}
  12. let maxCount = 128
  13. let minCount = 4
  14. func validateUsername(_ username: String) -> ValidationResult {
  15. if username.isEmpty {
  16. return .failed(message: "用户名不能为空")
  17. }
  18. if username.length < minCount || username.length > maxCount {
  19. return .failed(message: "用户名为4-128位字符")
  20. }
  21. let fristCharRegex = "^([a-zA-Z0-9])(.*)$"
  22. let fristCharPredicate = NSPredicate(format: "SELF MATCHES %@", fristCharRegex)
  23. if !fristCharPredicate.evaluate(with: username) {
  24. return .failed(message: "用户名以字母或数字开头")
  25. }
  26. if username.isContainsChinese {
  27. return .failed(message: "用户名不能包含中文字符")
  28. }
  29. if username.isExpectations {
  30. return .ok
  31. }
  32. return .failed(message: "用户名包含非法字符")
  33. }
  34. func validatePassword(_ password: String) -> ValidationResult {
  35. if password.isEmpty {
  36. return .failed(message: "密码不能为空")
  37. }
  38. if password.length < minCount || password.length > maxCount {
  39. return .failed(message: "密码为4-128位字符")
  40. }
  41. return .ok
  42. }
  43. }