Data Validation in Swift with Validated

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 Validated and why is it needed?

Validated is a lightweight library for Swift, designed for declarative data validation. Instead of writing cumbersome if-else chains or using NSPredicate, you describe validation rules as a method chain. This is especially useful when processing forms, API responses, and user input in iOS/macOS applications.

How to install Validated?

The library is distributed via Swift Package Manager. Add the dependency to Package.swift:

dependencies: [    .package(url: "https://github.com/your-repo/Validated.git", from: "1.0.0")]

Or via Xcode: File → Add Packages → paste the repository URL.

Main features

  • String validation — email, length, regular expressions checking.
  • Number validation — range, minimum/maximum value.
  • Composite checks — combining multiple rules via AND/OR.
  • Localized errors — automatic translation of messages to the device language.
  • Async validation — for checks requiring network requests.

Swift code example (iOS)

import Validated

struct RegistrationForm { @Validated(.email, .nonEmpty) var email: String @Validated(.range(6...20), .alphanumeric) var password: String func validate() -> Bool { return $email.isValid && $password.isValid }}

// Usagevar form = RegistrationForm()form.email = "user@example.com"form.password = "Pass123"

if form.validate() { print("Form is valid")} else { print("Error: \\(form.$email.errors.first ?? "")")}

When to use Validated?

The library is ideal for:

  • Registration and authorization forms
  • Real-time input field validation
  • Data verification before sending to the server
  • Creating clean code with separation of concerns

Use Validated if you want to reduce boilerplate code and make validation transparent and testable. The library is actively maintained and compatible with iOS 13+ and macOS 10.15+.

Recommendations