@molecule/app-forms
Core interface · forms · App (browser) · v2.4.0 · Apache-2.0
Form handling interface for molecule.dev
npm install @molecule/app-formsHow it works
@molecule/app-forms is the forms core interface on the app (browser) side: the API your app calls, with no vendor inside.
Bond a provider to choose the implementation.
import { createForm } from '@molecule/app-forms'
// (React apps: use `useForm` from `@molecule/app-react` — it takes these same
// options and creates the controller through the bonded forms provider.)
const form = createForm<{ email: string; password: string }>({
defaultValues: { email: '', password: '' },
mode: 'onBlur',
})
const email = form.register({
name: 'email',
required: t('forms.required', undefined, { defaultValue: 'This field is required' }),
email: true,
})
// Wire email.value / email.onChange / email.onBlur to your input element.
const onSubmit = form.handleSubmit(async (values) => {
await http.post('/signup', values) // relative path via the app HTTP client
})
// A rejected submit remains in form.getState().submitError and still rejects
// for programmatic callers. In React, <Form onSubmit={handleSubmit(save)}>
// from @molecule/app-ui-react renders the server error list automatically.Works with: @molecule/app-bond, @molecule/app-i18n
Reference
Form handling interface for molecule.dev.
Provides a unified form management API that works across different form libraries (native, React Hook Form, Formik, etc.).
Auto-generated, AI-first package reference for the molecule.dev ecosystem. It is written to be read by coding agents as much as by people, and is generated from this package's source — edit
src/index.tsJSDoc, not this file.
Quick Start
import { createForm } from '@molecule/app-forms'
// (React apps: use `useForm` from `@molecule/app-react` — it takes these same
// options and creates the controller through the bonded forms provider.)
const form = createForm<{ email: string; password: string }>({
defaultValues: { email: '', password: '' },
mode: 'onBlur',
})
const email = form.register({
name: 'email',
required: t('forms.required', undefined, { defaultValue: 'This field is required' }),
email: true,
})
// Wire email.value / email.onChange / email.onBlur to your input element.
const onSubmit = form.handleSubmit(async (values) => {
await http.post('/signup', values) // relative path via the app HTTP client
})
// A rejected submit remains in form.getState().submitError and still rejects
// for programmatic callers. In React, <Form onSubmit={handleSubmit(save)}>
// from @molecule/app-ui-react renders the server error list automatically.
Type
core
Installation
npm install @molecule/app-forms @molecule/app-bond @molecule/app-i18n
API
Interfaces
ValidationRule
Field validation rule.
interface ValidationRule {
/**
* Rule type.
*/
type:
'required' | 'min' | 'max' | 'minLength' | 'maxLength' | 'pattern' | 'email' | 'url' | 'custom'
/**
* Rule value (for rules like min, max, pattern).
*/
value?: unknown
/**
* Error message when validation fails.
*/
message: string
}
ValidationSchema
Field validation schema.
interface ValidationSchema {
/**
* Whether the field is required. A string turns the rule on and is its message; an empty
* string (an untranslated `t(...)`) still turns it on and shows the built-in message.
*/
required?: boolean | string
/**
* Minimum value (for numbers).
*/
min?: number | { value: number; message: string }
/**
* Maximum value (for numbers).
*/
max?: number | { value: number; message: string }
/**
* Minimum length (for strings).
*/
minLength?: number | { value: number; message: string }
/**
* Maximum length (for strings).
*/
maxLength?: number | { value: number; message: string }
/**
* Pattern to match (regex).
*/
pattern?: RegExp | { value: RegExp; message: string }
/**
* Validate as email. A string turns the rule on and is its message (empty: the built-in one).
*/
email?: boolean | string
/**
* Validate as URL. A string turns the rule on and is its message (empty: the built-in one).
*/
url?: boolean | string
/**
* Custom validation function. Return `true` to pass; `false` or any string fails, and a
* non-empty string is the message shown.
*/
validate?: (value: unknown) => boolean | string | Promise<boolean | string>
}
FieldState
Reactive state for a single form field (value, validation errors, touched/dirty flags).
interface FieldState<T = unknown> {
/**
* Current field value.
*/
value: T
/**
* Error message (if any).
*/
error?: string
/**
* Whether the field has been touched.
*/
touched: boolean
/**
* Whether the field is dirty (value changed from initial).
*/
dirty: boolean
/**
* Whether the field is valid.
*/
valid: boolean
/**
* Whether the field is currently being validated: true while the latest
* validation that covers it (the field's own, the whole form's, or a
* submission's) is still running.
*/
validating: boolean
}
FormState
Aggregate state of an entire form (all field values, errors, and submission status).
interface FormState<T extends FormValuesConstraint<T> = Record<string, unknown>> {
/**
* Form values.
*/
values: T
/**
* Field errors.
*/
errors: Partial<Record<keyof T, string>>
/**
* Touched fields.
*/
touched: Partial<Record<keyof T, boolean>>
/**
* Whether the form is valid.
*/
isValid: boolean
/**
* Whether the form is dirty.
*/
isDirty: boolean
/**
* Whether the form is submitting.
*/
isSubmitting: boolean
/**
* The latest rejected submission, cleared by a new submit, reset or clearErrors.
* Kept separately from client field validation; use the framework Form to render it.
*/
submitError?: unknown
/**
* Number of times the form has been submitted.
*/
submitCount: number
}
RegisterOptions
Field registration options.
interface RegisterOptions extends ValidationSchema {
/**
* Field name.
*/
name: string
/**
* Default value.
*/
defaultValue?: unknown
/**
* Value transformation on change.
*/
transform?: (value: unknown) => unknown
/**
* Dependencies for validation.
*/
deps?: string[]
}
FieldRegistration
Field registration result (for native inputs).
interface FieldRegistration {
/**
* Field name.
*/
name: string
/**
* Field value.
*/
value: unknown
/**
* Change handler.
*/
onChange: (event: { target: { value: unknown; name: string } } | unknown) => void
/**
* Blur handler.
*/
onBlur: () => void
/**
* Reference setter (for DOM elements).
*/
ref?: (element: HTMLElement | null) => void
}
FormController
Form controller interface.
All form providers must implement this interface.
interface FormController<T extends FormValuesConstraint<T> = Record<string, unknown>> {
/**
* Gets the current form state.
*/
getState(): FormState<T>
/**
* Gets the value of a specific field.
*/
getValue(name: string): unknown
getValue<K extends keyof T>(name: K): T[K]
/**
* Gets all form values.
*/
getValues(): T
/**
* Sets the value of a specific field.
*/
setValue(
name: string,
value: unknown,
options?: { shouldValidate?: boolean; shouldDirty?: boolean; shouldTouch?: boolean },
): void
/**
* Sets multiple values at once.
*/
setValues(values: Partial<T>, options?: { shouldValidate?: boolean }): void
/**
* Gets the error for a specific field.
*/
getError(name: string): string | undefined
/**
* Sets the error for a specific field.
*/
setError(name: string, error: string | undefined): void
/**
* Clears the error for a specific field.
*/
clearError<K extends keyof T>(name: K): void
/**
* Clears all errors.
*/
clearErrors(): void
/**
* Gets the field state for a specific field.
*/
getFieldState<K extends keyof T>(name: K): FieldState<T[K]>
/**
* Registers a field for form management. A `defaultValue` that fills an
* empty field is announced to subscribers once, in a microtask, so a field
* registered during a render does not update other components mid-render.
*/
register(nameOrOptions: string | RegisterOptions, options?: RegisterOptions): FieldRegistration
/**
* Unregisters a field.
*/
unregister(name: string): void
/**
* Validates a specific field.
*/
validateField<K extends keyof T>(name: K): Promise<boolean>
/**
* Validates all fields.
*/
validate(): Promise<boolean>
/**
* Resets the form and clears errors, touched fields and submission state.
*
* With no argument it restores the current baseline — the initial
* `defaultValues`, or the values a previous `reset(values)` established.
* Passing `values` merges them over the current baseline and makes the
* result the new baseline, so a form reset to freshly loaded or saved data
* reads clean (`isDirty === false`). A changed non-null `id` replaces the
* record and cancels a pending submit.
*/
reset(values?: Partial<T>): void
/**
* Applies externally loaded values without clearing submission state.
*
* The new clean baseline is the original `defaultValues` plus this latest
* record, never a merge of earlier records. With `keepDirtyValues`, edited
* fields retain their values while pristine fields take the loaded values.
* A changed non-null `id` still replaces the record and cancels a pending
* submit. Equal-content baselines cause no update. `keepOmittedValues` opts
* into incremental top-level field fills using the current clean baseline.
*/
updateValues(values: Partial<T>, resetOptions?: FormUpdateOptions): void
/**
* Handles form submission.
*/
handleSubmit(
onSubmit: (values: T) => void | Promise<void>,
onError?: (errors: Partial<Record<keyof T, string>>) => void,
): (event?: { preventDefault?: () => void }) => Promise<void>
/**
* Sets focus to a field.
*/
setFocus(name: keyof T): void
/**
* Subscribes to form state changes.
*/
subscribe(callback: (state: FormState<T>) => void): () => void
/**
* Destroys the form controller.
*/
destroy(): void
}
FormProvider
Form provider interface.
Implementations create form controllers.
interface FormProvider {
/**
* Creates a new form controller.
*/
createForm<T extends FormValuesConstraint<T>>(options: FormOptions<T>): FormController<T>
}
FormResetOptions
Options controlling how externally supplied values replace a form's baseline.
interface FormResetOptions {
/**
* Keep fields whose current value differs from their baseline when `values`
* changes. Pristine fields still take the new values. False by default, so a
* new record can replace edits from the previous record.
*/
keepDirtyValues?: boolean
}
FormUpdateOptions
Options for applying a loaded record or incrementally loaded fields.
interface FormUpdateOptions extends FormResetOptions {
/**
* Preserve the current clean baseline for omitted top-level fields. Used for
* independent field fills; false by default for whole-record replacements.
* A changed non-null record id always starts from the original defaults.
*/
keepOmittedValues?: boolean
}
FormOptions
Form creation options.
interface FormOptions<T extends FormValuesConstraint<T>> {
/**
* Default values, captured as the form's clean baseline when the controller
* is created. Mutating this object afterwards does not change the baseline.
*/
defaultValues?: Partial<T>
/**
* Externally loaded values, merged over `defaultValues` as the clean
* baseline. Framework bindings also apply later content changes; use
* `resetOptions.keepDirtyValues` to preserve edits while pristine fields fill.
*/
values?: Partial<T>
/** Controls how framework bindings apply changed `values`. */
resetOptions?: FormResetOptions
/**
* Validation mode.
*/
mode?: 'onSubmit' | 'onChange' | 'onBlur' | 'all'
/**
* Revalidation mode.
*/
reValidateMode?: 'onChange' | 'onBlur' | 'onSubmit'
/**
* Whether to focus the first error field on submit.
*/
shouldFocusError?: boolean
/**
* Form-level validation function.
*/
validate?: (
values: T,
) => Partial<Record<keyof T, string>> | Promise<Partial<Record<keyof T, string>>>
}
Types
FormValuesConstraint
The constraint every form entrypoint puts on a values type: an object type of
named fields (FormValuesShape), and object for any.
It exists so an app can name its form fields once, as an ordinary interface,
and pass that type whole — interface Fields { name: string; bio: string }
compiles the same as the type literal it replaces, while a primitive, an
array, a function and a Date are still rejected, and keyof T still types
register/get/set per field.
import type { FormOptions } from '@molecule/app-forms'
interface Fields {
name: string
bio: string
}
const options: FormOptions<Fields> = { defaultValues: { name: '', bio: '' } }
Forwarding a values type through your own generic copies this constraint, so the values keep being checked:
import type { FormOptions, FormValuesConstraint } from '@molecule/app-forms'
export function makeOptions<T extends FormValuesConstraint<T>>(o: FormOptions<T>) {
return o
}
T extends object is not enough: the entrypoints also reject arrays,
functions and Date, so a forwarder written that way does not compile. Copy
this type instead.
type FormValuesConstraint<T> = FormValuesIsRejected<T> extends true ? FormValuesRejection : object
Functions
createNativeFormProvider()
Creates a native form provider that manages form state, validation, and field registration without any external library. This is the built-in default used when no form library bond is configured.
Default validation messages are translated through @molecule/app-i18n's t when they
are produced, so they follow the bonded locale.
function createNativeFormProvider(): FormProvider
Returns: A FormProvider backed by vanilla JavaScript state management.
setProvider(provider)
Registers a form provider as the active singleton.
function setProvider(provider: FormProvider): void
provider— The form provider implementation to bond.
getProvider()
Retrieves the bonded form provider. If none is bonded, automatically creates and bonds the built-in native form provider.
function getProvider(): FormProvider
Returns: The active form provider.
hasProvider()
Checks whether a form provider has been explicitly bonded.
function hasProvider(): boolean
Returns: true if a form provider is bonded.
createForm(options)
Creates a new form controller for the given options using the active form provider. The controller manages field values, validation, dirty tracking, and submission.
function createForm<T extends FormValuesConstraint<T>>(options: FormOptions<T>): FormController<T>
options— Form configuration including initial values, validation rules, and submit handler.
Returns: A form controller instance for managing the form lifecycle.
snapshotFormValue(value)
Detach content-comparable form values recursively. Arrays retain holes, plain
objects and dates are copied, and opaque objects retain their identity just as
they do for formValuesEqual.
function snapshotFormValue<T>(value: T): T
formValuesEqual(left, right)
Compare form values recursively by content only for arrays, plain objects and dates. Other objects (including files, blobs, maps and class instances) use identity, so two distinct opaque values are never treated as interchangeable.
function formValuesEqual(left: unknown, right: unknown): boolean
validateValue(value, schema, t)
Validates a value against a validation schema.
When a translation function t is provided, default validation messages
will be passed through it for i18n support.
function validateValue(
value: unknown,
schema: ValidationSchema,
t?: TranslateFn,
): Promise<string | undefined>
value— The value to validate (string, number, array, or any type accepted by custom validators).schema— The validation rules to check against (required, min/max, pattern, email, etc.).t— Optional i18n translation function for localizing error messages.
Returns: The first validation error message, or undefined if the value passes all checks.
Constants
nativeProvider
Pre-instantiated native form provider, ready to use without calling createNativeFormProvider().
const nativeProvider: FormProvider
Injection Notes
Requirements
Peer dependencies:
@molecule/app-bond^1.0.1@molecule/app-i18n^1.1.2
Runtime Dependencies
@molecule/app-bond@molecule/app-i18n
Build forms with createForm (or the framework hook), not a direct react-hook-form /
formik import — that couples you to one library and breaks the swap.
- For independent loaded fields, call
updateValues({ email }, { keepDirtyValues: true, keepOmittedValues: true }). Omitted fields retain their latest clean baseline, and edited values remain dirty. FrameworkasState(field)fill setters do this automatically. Whole-form fills and ordinaryupdateValuesreplace the record from its original defaults, so fields omitted by a new record cannot leak. - Client validation is UX, NOT a security boundary.
validateValue/ client rules give instant feedback, but the SERVER must re-validate every field it receives — a request can skip the form entirely (curl, a tampered client). Never trust a value because the client "validated" it, and never enforce authorization in the form. - The built-in rules' default messages (
required: true,email: true, …) go through the bonded i18n provider'stunderforms.*keys, so bond@molecule/app-locales-formsto show them in the active locale. A rule's own message string is shown as written. - Keep secrets out of any form state you persist (see
@molecule/app-storage), and submit through the HTTP client (@molecule/app-http) with a relative path — never a hardcoded URL.
E2E Tests
Integration checklist — drive the real UI (live preview, no mocks), adapt each item to this app's actual screens/flows, and check every box off one by one. A box you can't check is an integration bug to fix — not a skip:
- Typing into each field updates its displayed value — interact_preview into the field's data-mol-id, then read_preview_ui shows the new value in the input (no stuck/blank input, no lag behind what you typed).
- Submitting with a required field empty, a malformed email, or an out-of-range number BLOCKS submit and shows that field's own error message beside it — the handler does not run (no navigation, success state, or POST).
- Fixing the offending field clears its error, and once every field is valid the same submit succeeds — a valid submit passes the correct current values (confirm the request/next screen carries what you typed, not stale or blank data).
- Errors appear at the configured time, not before: with mode onBlur or onSubmit a pristine, untouched field shows NO error on first render — the error only surfaces after you blur/touch it or attempt submit. No field screams before the user has interacted.
- Cross-field / form-level rules fire (e.g. a confirm-password mismatch via the form-level validate) and block submit until satisfied, showing the message on the right field.
- If the form does async validation (e.g. a username-taken check), submit waits for it to resolve before running the handler, any pending/validating indicator shows while it is in flight, and an async failure blocks submit with its message.
- Resetting the form restores the initial default values in the inputs and clears every error and touched state — a previously-shown error is gone and the submit control returns to its initial enabled/disabled state.
- Dirty/touched tracking is observable: an unchanged form reads as pristine (no "unsaved changes" affordance; save disabled if the app gates on dirty), editing a field flips it to dirty, and an invalid submit focuses the first error field.
Translations
Translation strings are provided by @molecule/app-locales-forms.