Password validation library for Swift with composable rules and localized error messages.
PasswordValidation provides type-safe password validation in Swift. It includes predefined validation rules and supports custom validation logic through dependency injection.
- Predefined validators for common password requirements
- Custom validation logic via closure-based composition
- Dependencies library integration for testability
- Localized error messages in English and Dutch
- Sendable conformance for Swift concurrency
Add the following to your Package.swift file:
dependencies: [
.package(url: "https://github.com/coenttb/swift-password-validation.git", from: "0.0.1")
]Then add PasswordValidation to your target dependencies:
.target(
name: "YourTarget",
dependencies: [
.product(name: "PasswordValidation", package: "swift-password-validation")
]
)import PasswordValidation
// Use the default validator
let validator = PasswordValidation.default
do {
let isValid = try validator.validate("MySecurePass123!")
print("Password is valid: \(isValid)")
} catch let error as PasswordValidation.Error {
print("Validation failed: \(error.description)")
}import Dependencies
import PasswordValidation
struct LoginService {
@Dependency(\.passwordValidation) var passwordValidation
func validateUserPassword(_ password: String) throws -> Bool {
return try passwordValidation.validate(password)
}
}The default validator implements standard security requirements:
- Length: 8-64 characters
- Uppercase: At least one uppercase letter (A-Z)
- Lowercase: At least one lowercase letter (a-z)
- Digits: At least one digit (0-9)
- Special Characters: At least one special character (
!&^%$#@()/)
let validator = PasswordValidation.default
try validator.validate("MySecurePass123!") // Returns trueThe simple validator requires only 4+ characters:
let validator = PasswordValidation.simple
try validator.validate("test") // Returns trueCreate your own validation rules:
let customValidator = PasswordValidation { password in
guard password.count >= 6 else {
throw PasswordValidation.Error.tooShort(minLength: 6)
}
guard !password.lowercased().contains("password") else {
throw PasswordValidation.Error.missingSpecialCharacter
}
return true
}The library provides specific error types for different validation failures:
do {
try PasswordValidation.default.validate("weak")
} catch PasswordValidation.Error.tooShort(let minLength) {
print("Password too short, needs at least \(minLength) characters")
} catch PasswordValidation.Error.missingUppercase {
print("Password needs an uppercase letter")
} catch PasswordValidation.Error.missingDigit {
print("Password needs a digit")
} catch {
print("Other validation error: \(error)")
}tooShort(minLength: Int)- Password is shorter than requiredtooLong(maxLength: Int)- Password exceeds maximum lengthmissingUppercase- No uppercase letters foundmissingLowercase- No lowercase letters foundmissingDigit- No digits foundmissingSpecialCharacter- No special characters found
- swift-translating: A Swift package for inline translations.
- coenttb-web: A Swift package with tools for web development building on swift-web.
- swift-server-foundation: A Swift package with tools to simplify server development.
- pointfreeco/swift-dependencies: A dependency management library for controlling dependencies in Swift.
This project is licensed under the Apache 2.0 License. See LICENSE for details.
Contributions are welcome. Please open an issue or submit a pull request.