@mitcsutt/kiln-forms
Version:
Kiln forms: typed React forms on TanStack Form, in component or JSON schema mode, rendered through kiln-ui
63 lines (62 loc) • 3.29 kB
TypeScript
import type { DefaultLayoutKey, UntypedFormSchema } from './types.js';
/** Registered keys: an array of names or the registry object itself (its keys are used). */
export type RegistryNames = readonly string[] | Readonly<Record<string, unknown>>;
/** What a server-driven schema may reference (§10.8). `layouts` adds to the default layouts. */
export interface SchemaRegistryNames {
kinds: RegistryNames;
layouts?: RegistryNames;
loaders?: RegistryNames;
validators?: RegistryNames;
computers?: RegistryNames;
nodes?: RegistryNames;
}
/** Options for `parseFormSchema`. */
export interface ParseFormSchemaOptions {
/**
* Accept literal `pattern` rules. Default `false`: a regex from untrusted JSON can take
* exponential time, and no heuristic catches every slow one. Pass `true` only for a trusted
* source (your own CMS, fixtures). `unsafePatternReason` then rejects the obvious slow shapes,
* but it is a heuristic: some slow patterns get past it (`^` + `\d{0,10}` ×10 + `x$`), so it is no
* substitute for trusting the source.
* Untrusted schemas use a registered named validator (`{ rule: 'custom', validator: 'email' }`).
*/
allowPatterns?: boolean;
}
/** The issue for a literal `pattern` rule when `allowPatterns` is off. */
export declare const PATTERN_NOT_ALLOWED = "pattern rules aren't allowed in parsed schemas; register a named validator, or pass allowPatterns for trusted sources";
export interface SchemaIssue {
/** Where, e.g. `root.children[2].kind`. */
path: string;
message: string;
}
export type ParseFormSchemaResult = {
ok: true;
schema: UntypedFormSchema;
} | {
ok: false;
issues: readonly SchemaIssue[];
};
/** The default layout keys (§9, §10.11) — always allowed. `repeater` is its own node shape. */
export declare const DEFAULT_LAYOUT_KEYS: readonly DefaultLayoutKey[];
/** Resource limits for untrusted schemas. Each breach is an issue, never a throw. */
export declare const SCHEMA_LIMITS: {
/** Deepest node nesting (root = 1), and deepest condition nesting (`all`/`any`/`not`). */
readonly maxDepth: 64;
/** Most nodes in one schema. */
readonly maxNodes: 2000;
/** Deepest nesting inside one JSON prop value (`defaultValue`, `newItem`, `options`, …). */
readonly maxJsonDepth: 64;
};
/**
* Validates untrusted JSON as a form schema against the kit's registered keys: node shapes,
* field kinds / layouts / loaders / validators / computers / custom nodes, rule names and argument
* types, condition shapes, duplicate ids and field names, repeater `newItem`, JSON-only props.
* Untrusted input: rejects DOM-sink props (`dangerouslySetInnerHTML`, `on*`, `style`,
* `javascript:` URLs, … — `unsafeProps.ts`), schemas over `SCHEMA_LIMITS`, and literal `pattern`
* rules unless `options.allowPatterns` (then slow-looking patterns are still rejected).
* Never throws on hostile JSON: every problem is an issue with its path.
* Hand-written (no zod at runtime). On success returns the same object, typed `UntypedFormSchema`.
*
* @privateRemarks Design reference §10.8.
*/
export declare function parseFormSchema(json: unknown, registry: SchemaRegistryNames, options?: ParseFormSchemaOptions): ParseFormSchemaResult;