Forms

SchemaForm

Schema-driven form that renders accessible controls from JSON Schema and submits one validated value object.

import { SchemaForm } from '@lostgradient/cinder/schema-form';
formschemavalidation
01

Overview

Render an accessible form from a JSON Schema object and submit one validated value.

Usage

svelte
<script lang="ts">
  import SchemaForm, { readSchemaFormData } from '@lostgradient/cinder/schema-form';

  const schema = {
    type: 'object',
    properties: {
      name: { type: 'string', minLength: 1 },
      count: { type: 'integer', minimum: 1 },
    },
    required: ['name', 'count'],
  };

  function save(value: unknown) {
    // Called only after validation passes.
  }

  function mirrorDraft(value: unknown) {
    // Called after every edit, even when the value is not schema-valid yet.
  }
</script>

<SchemaForm {schema} name="payload" onDraftChange={mirrorDraft} onsubmit={save} />

SchemaForm always renders a real <form> and a hidden serialized output field. After valid submission, readSchemaFormData(new FormData(form), 'payload') returns the same object delivered to onsubmit.

The three value channels have distinct ownership. value seeds internal state on mount and is not a live binding, so later value changes do not discard active edits. onDraftChange reports the complete current draft after each edit, before schema validation, so consumers can mirror nested or temporarily invalid values. In-progress numeric editors and temporarily unparseable JSON fields are reported as their current raw text until the value commits or parses successfully. onsubmit reports only schema-valid output after submission.

Live preview
02

When to use

Use when
  • Capturing a payload whose shape is already described by a workflow, API, or JSON Schema document.
  • You need callback submission and native FormData submission to expose the same validated object.
Avoid when
  • Authoring or editing a JSON Schema document — use json-schema-editor instead.
  • You need bespoke multi-step flows or custom cross-field user interface beyond schema validation.
03

Examples

JSON Schema

Render and submit a payload from a JSON Schema object.

04

Props

Props for schema-form
Name Type Default Required Bindable Description
schema Schema req JSON Schema object used to render and validate the form.
05

Accessibility

Enter
Submits the form from text-like controls.
Invalid submission moves focus to the first invalid field and associates each field error through aria-describedby.