Skip to content

Form Validation

ThemeKit splits validation into two layers: a pure logic layer you can unit test in isolation, and a SwiftUI presentation layer that surfaces results inline on your inputs.

Validator, ValidationRule, and the built-in Validators compose into rules that return a result you can drive UI from. Rules are extensible — plain predicates, regular expressions, or async checks (e.g. server-side uniqueness).

let rule = ValidationRule.all(
Validators.required(message: "Email is required"),
Validators.email(message: "Enter a valid email")
)
let result = rule.validate(emailField) // pure, synchronous, testable

Bind a rule to an input and ThemeKit renders the error state and message using the active theme’s semantic colors and spacing — consistent with the rest of your UI.

TextInput("Email", text: $email)
.validation(rule, message: $emailError)

Add your own predicate or regex rule, or an async validator for checks that need the network:

let unique = Validator.async { value in
await api.isEmailAvailable(value) ? .valid : .invalid("Already taken")
}