Created
May 2, 2024 14:26
-
-
Save omidgolparvar/45cc7436ad9cf9ea30912b08cc40ed9c to your computer and use it in GitHub Desktop.
This file contains 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
public enum PasswordRule { | |
public enum CharacterClass { | |
case upper | |
case lower | |
case digits | |
case special | |
case asciiPrintable | |
case unicode | |
case custom(Set<Character>) | |
} | |
case required(CharacterClass) | |
case allowed(CharacterClass) | |
case maxConsecutive(UInt) | |
case minLength(UInt) | |
case maxLength(UInt) | |
public static func descriptor(for rules: [PasswordRule]) -> String { | |
rules.map { "\($0.description);" }.joined(separator: " ") | |
} | |
} | |
extension PasswordRule: CustomStringConvertible { | |
public var description: String { | |
switch self { | |
case .required(let characterClass): | |
return "required: \(characterClass)" | |
case .allowed(let characterClass): | |
return "allowed: \(characterClass)" | |
case .maxConsecutive(let length): | |
return "max-consecutive: \(length)" | |
case .minLength(let length): | |
return "minlength: \(length)" | |
case .maxLength(let length): | |
return "maxlength: \(length)" | |
} | |
} | |
} | |
extension PasswordRule.CharacterClass: CustomStringConvertible { | |
public var description: String { | |
switch self { | |
case .upper: return "upper" | |
case .lower: return "lower" | |
case .digits: return "digits" | |
case .special: return "special" | |
case .asciiPrintable: return "ascii-printable" | |
case .unicode: return "unicode" | |
case .custom(let characters): | |
return "[" + String(characters) + "]" | |
} | |
} | |
} | |
#if canImport(UIKit) | |
import UIKit | |
extension UITextInputPasswordRules { | |
convenience init(rules: [PasswordRule]) { | |
let descriptor = PasswordRule.descriptor(for: rules) | |
self.init(descriptor: descriptor) | |
} | |
} | |
#endif | |
extension UITextInputPasswordRules { | |
static var blu: UITextInputPasswordRules { | |
UITextInputPasswordRules(rules: [ | |
.required(.lower), | |
.required(.upper), | |
.required(.digits), | |
.minLength(12), | |
.maxLength(16) | |
]) | |
} | |
} | |
print(UITextInputPasswordRules.blu.passwordRulesDescriptor) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment