coenttb/swift-password-validation
A Swift package for type-safe password validation.
Overview
PasswordValidation provides type-safe password validation in Swift. It includes predefined validation rules and supports custom validation logic through dependency injection.
Features
- 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
Installation
Swift Package Manager
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")
]
)Quick Start
Basic Usage
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)")
}With Dependencies
import Dependencies
import PasswordValidation
struct LoginService {
@Dependency(\.passwordValidation) var passwordValidation
func validateUserPassword(_ password: String) throws -> Bool {
return try passwordValidation.validate(password)
}
}Usage Examples
Default Validator
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 trueSimple Validator
The simple validator requires only 4+ characters:
let validator = PasswordValidation.simple
try validator.validate("test") // Returns trueCustom Validation
Create 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
}Error Handling
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)")
}Available Errors
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
License
This project is licensed under the Apache 2.0 License. See LICENSE for details.
Contributing
Contributions are welcome. Please open an issue or submit a pull request.
Package Metadata
Repository: coenttb/swift-password-validation
Homepage: https://coenttb.com
Stars: 2
Forks: 0
Open issues: 2
Default branch: main
Primary language: swift
License: Apache-2.0
Topics: composable-validation, password, password-strength, password-validation, security, swift, type-safe, validation
README: README.md