How deference3/form-doctor works
Purpose and module layout
FormDoc is a Kotlin Multiplatform, Compose-oriented form-validation library. It combines runtime form state management (formdoc), compile-time KSP metadata generation (processor), and a Compose Multiplatform sample consumer (composeApp). Root Gradle configuration assembles these modules: settings.gradle.kts, build.gradle.kts.
Runtime validation core
The formdoc module defines the portable validation contract and reactive state model. FormState owns a model instance plus metadata, exposes validation/submission operations, and updates per-property FieldState; FormScope supports Compose-facing form content. Annotation declarations express built-in and custom constraints, while validators execute them.
Paths: formdoc/src/commonMain/kotlin/me/deference/formdoc/FormState.kt, formdoc/src/commonMain/kotlin/me/deference/formdoc/FieldState.kt, formdoc/src/commonMain/kotlin/me/deference/formdoc/ValidationAnnotations.kt, formdoc/src/commonMain/kotlin/me/deference/formdoc/Validators.kt.
Compile-time metadata pipeline
Consumers annotate data classes/properties, then KSP invokes the registered provider and processor. The processor scans validatable symbols and generates typed FormMetadata implementations mapping model properties to validator definitions. This is the key boundary: validation rules are discovered at build time, avoiding runtime reflection.
Paths: processor/src/main/kotlin/me/deference/processor/FormProcessorProvider.kt, processor/src/main/kotlin/me/deference/processor/FormProcessor.kt, processor/src/main/resources/META-INF/services/com.google.devtools.ksp.processing.SymbolProcessorProvider.
Metadata registry boundary
Generated metadata must be surfaced to common Compose code through an application-owned expect registry. The sample declares this registry in commonMain; its forms request metadata by model type before creating form state. This manual integration exists because generated code is not directly placed in common source sets.
Paths: composeApp/src/commonMain/kotlin/formdoc/registry/FormMetadataRegistry.kt, composeApp/src/commonMain/kotlin/me/deference/formsample/customer/CustomerFormModel.kt, composeApp/src/commonMain/kotlin/me/deference/formsample/customer/CustomerForm.kt.
Sample UI and form flow
The sample is the reference integration: app startup renders Compose UI, customer form components bind field values to reactive field state, selector/address widgets collect nested input, and submission invokes validation callbacks. Customer model annotations and master data drive the demonstrated validation and selection behavior.
Paths: composeApp/src/commonMain/kotlin/me/deference/formsample/App.kt, composeApp/src/commonMain/kotlin/me/deference/formsample/customer/CustomerForm.kt, composeApp/src/commonMain/kotlin/me/deference/formsample/customer/components/AddressForm.kt.
Platform entry points and runtimes
Shared Kotlin/Compose code targets Android, iOS, JVM desktop, JS/web, and Wasm JS through platform-specific actual platform implementations. Android starts from an Activity; iOS bridges Compose through a view controller and an Xcode host; JVM and web have explicit main functions. The library itself supplies Android, iOS, JVM, and web platform source sets.
Paths: composeApp/src/androidMain/kotlin/me/deference/formsample/MainActivity.kt, composeApp/src/iosMain/kotlin/me/deference/formsample/MainViewController.kt, composeApp/src/jvmMain/kotlin/me/deference/formsample/main.kt, composeApp/src/webMain/kotlin/me/deference/formsample/main.kt.
Delivery and external build infrastructure
Gradle manages Kotlin Multiplatform, Compose, and KSP compilation; the processor is consumed as a KSP dependency by downstream projects. GitHub Actions publishes artifacts, making the runtime library and annotation processor separately distributable.
Paths: formdoc/build.gradle.kts, processor/build.gradle.kts, composeApp/build.gradle.kts, .github/workflows/publish.yml.
Main entry points: App.kt Sample App · FormProcessor.kt FormProcessor · CustomerForm.kt CustomerForm · CustomerFormModel.kt CustomerFormModel
Components 20
Runtime library
- Validation annotations · Built-in and custom validation constraints.
formdoc/src/commonMain/kotlin/me/deference/formdoc/ValidationAnnotations.kt - Validators
formdoc/src/commonMain/kotlin/me/deference/formdoc/Validators.kt - FormState · Owns the model, metadata, validation, and submission flow.
formdoc/src/commonMain/kotlin/me/deference/formdoc/FormState.kt - FieldState
formdoc/src/commonMain/kotlin/me/deference/formdoc/FieldState.kt
Metadata generation
- KSP provider registration
processor/src/main/resources/META-INF/services/com.google.devtools.ksp.processing.SymbolProcessorProvider - FormProcessorProvider
processor/src/main/kotlin/me/deference/processor/FormProcessorProvider.kt - FormProcessor · Generates typed FormMetadata without runtime reflection.
processor/src/main/kotlin/me/deference/processor/FormProcessor.kt - Generated FormMetadata · Typed property-to-validator metadata implementations.
Sample integration
- CustomerFormModel
composeApp/src/commonMain/kotlin/me/deference/formsample/customer/CustomerFormModel.kt - FormMetadataRegistry · Application-owned bridge from common code to generated metadata.
composeApp/src/commonMain/kotlin/formdoc/registry/FormMetadataRegistry.kt - CustomerForm · Obtains metadata, creates form state, and handles submission.
composeApp/src/commonMain/kotlin/me/deference/formsample/customer/CustomerForm.kt - AddressForm
composeApp/src/commonMain/kotlin/me/deference/formsample/customer/components/AddressForm.kt - Sample App
composeApp/src/commonMain/kotlin/me/deference/formsample/App.kt
Platform delivery
- Platform entry points · Platform hosts launch shared Compose UI.
- Android Activity
composeApp/src/androidMain/kotlin/me/deference/formsample/MainActivity.kt - iOS ViewController
composeApp/src/iosMain/kotlin/me/deference/formsample/MainViewController.kt - JVM main
composeApp/src/jvmMain/kotlin/me/deference/formsample/main.kt - Web main
composeApp/src/webMain/kotlin/me/deference/formsample/main.kt - Gradle build · Builds runtime, processor, sample, and KSP integration.
composeApp/build.gradle.kts - Artifact publishing · Publishes runtime and processor artifacts separately.
.github/workflows/publish.yml
Connections 26
- Validation annotations → FormProcessor: scanned by
- KSP provider registration → FormProcessorProvider: registers
- FormProcessorProvider → FormProcessor: creates
- FormProcessor → Generated FormMetadata: generates
- CustomerFormModel → Validation annotations: uses
- CustomerFormModel → FormProcessor: processed by KSP
- Generated FormMetadata → FormMetadataRegistry: surfaced through
- FormMetadataRegistry → CustomerForm: provides metadata
- CustomerFormModel → CustomerForm: edited by
- CustomerForm → FormState: creates
- FormState → FieldState: updates
- FormState → Validators: runs
- Generated FormMetadata → Validators: defines rules for
- AddressForm → CustomerForm: nested in
- Sample App → CustomerForm: renders
- Android Activity → Sample App: launches
- iOS ViewController → Sample App: hosts
- JVM main → Sample App: launches
- Web main → Sample App: launches
- Platform entry points → Android Activity: includes
- Platform entry points → iOS ViewController: includes
- Platform entry points → JVM main: includes
- Platform entry points → Web main: includes
- Gradle build → FormProcessor: compiles
- Gradle build → Sample App: builds
- Artifact publishing → Gradle build: runs