Data Validation in Swift: Validator Library

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

What is data validation in Swift and why is it needed?

Data validation is the process of checking input data against specified rules. In iOS/macOS development, it is critically important: from checking email and password in a registration form to validating a JSON response from the server. Built-in Foundation tools (NSPredicate, regular expressions) are often cumbersome and inconvenient for reuse. Validator is a lightweight library for Swift that allows you to declaratively describe validation rules using chainable syntax and use them anywhere in the project.

How to install the Validator library

Validator is available via Swift Package Manager (SPM). Add it to Package.swift or through Xcode: File → Add Packages → enter the repository URL:

https://github.com/adamwaite/Validator

Or via CocoaPods, if you use it:

pod 'Validator'

Main features of the library

  • Built-in rules: email, URL, string, number, length, regular expressions, and many others.
  • Custom rules: you can create any check by implementing the ValidationRule protocol.
  • Collection validation: checking arrays, dictionaries, optional values.
  • Human-readable errors: returns localized error messages.
  • UIKit/SwiftUI support: you can directly validate text fields.

Swift code example

import Validator

// Create ruleslet emailRule = ValidationRulePattern(pattern: EmailValidationPattern.standard)let passwordRule = ValidationRuleLength(min: 8, max: 32)

// Validate a stringlet email = "test@example.com"let result = email.validate(rule: emailRule)

switch result {case .valid: print("Email is valid")case .invalid(let errors): print("Errors: \\(errors)")}

// Validation with multiple ruleslet username = "user123"let usernameRules = ValidationRuleSet<String>()usernameRules.add(rule: ValidationRuleLength(min: 3))usernameRules.add(rule: ValidationRulePattern(pattern: "^[a-zA-Z0-9_]+$"))

let usernameResult = username.validate(rules: usernameRules)if case .invalid(let errors) = usernameResult { print("Username validation errors: \\(errors)")}

// Example for UITextField (UIKit)let textField = UITextField()textField.text = "invalid email"textField.validate(rule: emailRule) { result in switch result { case .valid: textField.layer.borderColor = UIColor.green.cgColor case .invalid(let errors): textField.layer.borderColor = UIColor.red.cgColor print(errors.first?.message ?? "") }}

When to use Validator

The library is ideal for:

  • Input forms (registration, login, profile).
  • Data validation before sending to the server.
  • Real-time user input validation.
  • Parsing and validating JSON/API responses.
  • Any scenarios requiring reusable data validation.

Validator significantly reduces boilerplate code, making validation readable and testable. It is a must-have tool for any Swift developer working with user input or external data.

Recommendations