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.
The logic layer
Section titled “The logic layer”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, testablePresentation
Section titled “Presentation”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)Extending
Section titled “Extending”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")}