Skip to content

coenttb/swift-password-validation

swift-password-validation

CI Development Status

Password validation library for Swift with composable rules and localized error messages.

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 true

Simple Validator

The simple validator requires only 4+ characters:

let validator = PasswordValidation.simple
try validator.validate("test") // Returns true

Custom 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 required
  • tooLong(maxLength: Int) - Password exceeds maximum length
  • missingUppercase - No uppercase letters found
  • missingLowercase - No lowercase letters found
  • missingDigit - No digits found
  • missingSpecialCharacter - No special characters found

Related Packages

Dependencies

Used By

Third-Party Dependencies

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.

Sponsor this project

 

Contributors 2

  •  
  •  

Languages