UNPKG

@mitcsutt/kiln-forms

Version:

Kiln forms: typed React forms on TanStack Form, in component or JSON schema mode, rendered through kiln-ui

39 lines (38 loc) • 2.34 kB
import type { StandardSchemaV1 } from '@tanstack/react-form'; import { type FormMessages } from '../../runtime/messages.js'; import type { NamedValidator, UntypedFormSchema } from './types.js'; /** * Empty values (§7.1) of default kinds that `required` must treat as empty beyond * `'' / null / undefined / []` — an unchecked checkbox or switch. */ export declare const DEFAULT_EMPTIES: Readonly<Record<string, unknown>>; export interface ToStandardSchemaOptions { /** The kit's validator registry (needed when the schema has `custom` rules). */ validators?: Readonly<Record<string, NamedValidator>>; /** The render context conditions read (`{ context: 'mode', … }`). */ context?: Record<string, unknown>; messages?: Partial<FormMessages>; /** kind → empty value for `required` (default `DEFAULT_EMPTIES`). */ empties?: Readonly<Record<string, unknown>>; } /** * The schema's rules as a Standard Schema, React-free: use it on the server * (`createServerValidate`, an API handler) so client and server enforce the same rules. * * Applies `rules` (+ `required` / `requiredWhen`) of **visible, active** fields only, using the same * `fieldFlags` as the renderer: nodes whose `when` is false are skipped with their subtree, as are * fields that are excluded, disabled or read-only — by their own static prop, a `*When` condition, * `compute`, or an ancestor `section`'s `disabled` / `readOnly` (`layoutFlags`). Those are the * fields the client doesn't validate. A repeater under such a section is skipped whole, and * so is every `review` subtree (view mode never validates; the field's own node counts). * Repeater rules apply to the array and item rules to each item. `warnRules` never fail. Returns a * promise only when async custom validators run. * * Success output holds only the schema's active fields: unknown keys and the * values of hidden, excluded, disabled, read-only and computed fields are dropped, never passed * through — the server must not trust them (recompute a computed value if it needs one). A * repeater keeps one object per input item with that item's active fields. * * @privateRemarks Design reference §10.9, §5.4. */ export declare function toStandardSchema(schema: UntypedFormSchema, opts?: ToStandardSchemaOptions): StandardSchemaV1<unknown, unknown>;