palmeiralabs/inputfence
InputFence is a lightweight and flexible Swift Package designed to simplify user input validation in any iOS, macOS, watchOS, or tvOS application.
β¨ Features
- β Prebuilt Validators β Quickly validate common input types.
- βοΈ Configurable Parameters β Adjust rules such as min/max lengths, allowed characters, and age ranges.
- π Protocol-Oriented Design β Easy to extend and create your own custom validators.
- π¦ Swift Package Manager Support β Simple integration into any project.
- π Locale-Aware β Handles formats like date of birth parsing with regional settings.
π¦ Installation
Swift Package Manager
- In Xcode, go to File > Add Packages...
- Enter the repository URL:
`` https://github.com/PalmeiraLabs/InputFence ``
- Select the version and add it to your project.
π Usage
Example β Validating a Date of Birth
import InputFence
let validator = AdvancedDateOfBirthValidator(
parameters: DateOfBirthValidatorParameters(minAge: 18, maxAge: 99)
)
if validator.isValid("12/08/1995") {
print("Valid date of birth β
")
} else {
print("Invalid date of birth β")
}Example β Validating a Username
import InputFence
let validator = UsernameValidator(
parameters: UsernameValidatorParameters(minLength: 3, maxLength: 20)
)
if validator.isValid("Agustin") {
print("Valid username β
")
} else {
print("Invalid username β")
}Usage examples
import InputFence
// Example 1: Validate an email using the simple validator
let emailValidator = SimpleEmailValidator()
print(emailValidator.isValid("user@example.com")) // true
print(emailValidator.isValid("invalid-email")) // false
// Example 2: Use an advanced password validator with custom parameters
let passwordParams = PasswordValidatorParameters(
minLength: 8,
requireUppercase: true,
requireNumber: true,
requireSpecialCharacter: true
)
let passwordValidator = AdvancedPasswordValidator(parameters: passwordParams)
print(passwordValidator.isValid("P@ssword1")) // true
// Example 3: Use the generic wrapper to validate a field
let wrappedEmail = EmailValidator(
validator: SimpleEmailValidator(),
value: "contact@domain.com"
)
print(wrappedEmail.isValid()) // true
// Example 4: Use the generic wrapper to validate passwords
let wrappedPassword = PasswordValidator(
validator: passwordValidator,
value: "1234"
)
print(wrappedPassword.isValid()) // falseTips
- You can instantiate a specific validator directly (e.g., SimpleEmailValidator().isValid(...)).
- You can wrap a specific validator inside your generic FieldValidator (e.g., EmailValidator(...) or PasswordValidator(...)).
- You can pass custom parameters (as in AdvancedPasswordValidator).
- FieldValidator<T> β Generic that accepts any validation protocol implementation.
- Typealiases (EmailValidator, PasswordValidator, etc.) β Typed versions for each data type.
- Protocols (EmailValidatorProtocol, etc.) β Define the validation rules.
- Concrete implementations (SimpleEmailValidator, AdvancedPasswordValidator, etc.) β Actual validation logic.
π Documentation
For detailed guides, API reference, and validator specifications, check out the full documentation:
- Online Docs β InputFence Documentation
- Local Docs
1. Clone this repository. 2. Install MkDocs (if you donβt have it): ``bash pip install mkdocs-material ` 3. Serve the documentation locally: `bash mkdocs serve ` 4. Open http://127.0.0.1:8000` in your browser.
Documentation source files are located in the docs/ directory and include:
- Validators β Usage of each built-in validator.
- Protocols β Interfaces and extension points.
- Parameters β Customization and configuration options.
π License
This project is licensed under the MIT License β see the LICENSE file for details.
π¬ Contact
Feel free to reach out with questions, feedback, or licensing inquiries:
π§ agustin.palmeira.it@gmail.com π https://www.linkedin.com/in/agustin-daniel-palmeira/
Package Metadata
Repository: palmeiralabs/inputfence
Default branch: main
README: README.md