# Schemas & Reactive Forms (crow-schema) (https://crow.build/docs/stack/schema)

`crow-schema` brings a concise, composable **Zod-style validation DSL** directly to Kotlin — paired with a fine-grained `FormState` where every field's value, error, touched, dirty, valid, and async-validating status is a reactive `Signal` / `Read`.

## Building Schemas [#building-schemas]

```kotlin
val emailSchema = Schemas.string()
    .trim()
    .email("Enter a valid email address")

val doseSchema = Schemas.int()
    .min(10, "Minimum dose is 10g")
    .max(40, "Maximum dose is 40g")

val titleSchema = Schemas.string()
    .trim()
    .min(1, "Title is required")
    .max(60, "Keep title under 60 characters")
```

Call `schema.safeParse(input)` anywhere (UI, network layer, tests) for typed `ParseResult.Success<T>` / `ParseResult.Failure` with structured error paths — or call `schema.toJsonSchema()` to emit standard JSON Schema definitions.

## Signal-Backed `FormState` [#signal-backed-formstate]

Bind schemas directly to Crow inputs so each field validates independently with zero whole-form recomposition:

```kotlin
@Ui
fun LogBrewForm(@Once onSaved: (String, String) -> Unit) {
    val form = FormState(mode = ValidationMode.OnChange)
    onCleanup { form.dispose() }

    val title = form.field("title", "", titleSchema)
    val notes = form.field("notes", "", Schemas.string().trim().max(240))

    // Cross-field validation runs reactively alongside individual fields
    form.crossField("notes", "Add notes when logging a custom blend") {
        !(title.value.value.contains("Blend", ignoreCase = true) && notes.value.value.isBlank())
    }

    Column(verticalArrangement = Arrangement.spacedBy(12.dp)) {
        OutlinedTextField(
            value = title.value,
            label = { Text("Brew title") },
            isError = title.error.value != null,
            supportingText = { Text(title.error.value ?: "") },
        )

        Button(
            onClick = { form.submit { onSaved(title.value.value, notes.value.value) } },
            enabled = form.isValid.value && !form.isSubmitting.value,
        ) {
            Text("Save Brew")
        }
    }
}
```

### Features [#features]

- **Validation modes**: `OnChange`, `OnBlur`, `OnTouched`, or `OnSubmit` per form.
- **Async field validators**: Attach `AsyncFieldValidator` (e.g., username availability checks) with automatic cancellation and `field.validating` status signal.
- **External errors**: Surface server-side validation messages directly onto any field with `field.setExternalError("Already taken")`.
- **Pure JVM**: `crow-schema` has zero Android dependencies (`checkCorePurity` verified).