svelte-jsonschema-form

repository·main·Indexed 41 days ago

https://github.com/x0k/svelte-jsonschema-form

A Svelte 5 library for generating dynamic forms based on JSON Schema. An unofficial port of react-jsonschema-form, it provides a customizable system for building forms with various themes (such as Beer CSS, daisyUI v5, Flowbite, and shadcn-svelte) and validation engines (including Ajv8, ATA, Hyperjump, and Cloudflare Workers).

Tokens
53.2K
Snippets
231
Records
348
Agent score
51%

What's inside svelte-jsonschema-form

  1. Use @sjsf/sveltekit with SvelteKit

    main

    The @sjsf/sveltekit package provides integration capabilities for svelte-jsonschema-form within SvelteKit applications. It supports two primary patterns for handling form data and logic:

    1. Form Actions: Use SvelteKit's native Form Actions to handle form submissions.
    2. Remote Functions: Use remote functions for more complex or decoupled logic.

    Refer to the official documentation for detailed implementation guides on these patterns.

  2. Understand the UI Schema concept

    main

    The UI Schema is a configuration object that follows the tree structure of your JSON Schema. It allows you to customize the appearance and behavior of form fields without modifying the underlying data schema.

    Key capabilities include:

    • Overriding UI components (widgets).
    • Providing custom UI options (e.g., labels, ordering, array behaviors).
    • Defining specific UI schemas for complex structures like oneOf, anyOf, items (arrays), and additionalProperties (objects).
    • Using $ref to point to shared definitions in ui:definitions.
  3. Understand the svelte-jsonschema-form project hierarchy

    main

    The project is organized into a hierarchical structure of packages and tools. The top-level hierarchy flows from the root through the code generation and composition layers, eventually reaching the playground and builder environments:

    root > codegen > composer > playground > builder

  4. Understand FormState queries and commands

    main

    SJSF provides two types of methods to interact with the FormState: Queries and Commands. Understanding the difference is critical for managing reactivity in Svelte.

    Queries (@query)

    Queries are used to read data from the form state. When a query is called, the reactive variables it accesses will be tracked by Svelte's reactivity system.

    Common Queries:

    • getFieldErrors, getErrors, hasErrors: Access validation state.
    • getValueSnapshot: Get the current value.
    • retrieveSchema, retrieveUiSchema: Access schema definitions.
    • getId, getIdByPath: Retrieve unique identifiers for fields.

    Commands (@command)

    Commands are used to modify the form state. When a command is called, the accessed variables will not be tracked by the reactivity system, preventing infinite loops or unintended side effects during state transitions.

    Common Commands:

    • setValue: Update a field's value.
    • validateField: Trigger validation for a specific field.
    • updateErrors, updateFieldErrors: Manually update error states.
    • setFieldStateByPath: Update field state directly.
  5. Identify versioning patterns for theme packages

    main

    Some theme packages use specific naming conventions based on the major version of their underlying dependencies to maintain clarity during transitions:

    • Flowbite Themes: @sjsf/flowbite3-theme is named after the major version of the flowbite package (even though its primary dependency is flowbite-svelte).
    • Shadcn Themes: @sjsf/shadcn4-theme is named after the major version of tailwindcss.
  6. Handle files using Native File objects

    main

    When using native-file or native-files fields, uploaded files are represented directly as File objects.

    Pros: No encoding/decoding overhead and metadata is preserved. Cons: Files cannot be provided from the server and may cause compatibility issues with JSON-oriented tools like JSON Schema validators.

    Requirement: You must set enctype="multipart/form-data" on your form.

  7. Understand the @sjsf and @sjsf-lab package scopes

    main

    The project uses two distinct npm scopes to signal the stability and versioning behavior of its packages:

    @sjsf-lab (Unstable/Experimental)

    • Purpose: Used for packages that are not yet stabilized.
    • Versioning: Packages are versioned independently.
    • Compatibility: The major version of a @sjsf-lab package matches the compatible version of @sjsf/form. Note that breaking changes may occur between minor versions.

    @sjsf (Stable)

    • Purpose: Used for stable, production-ready packages.
    • Versioning: Packages are versioned in sync. Packages sharing the same version number are guaranteed to be compatible with each other.
    • Deprecation: When a stable package is deprecated (e.g., replaced by a newer version), it remains in the @sjsf scope but switches to independent versioning.
  8. Preload files into storage

    main

    To avoid the overhead of Data URLs and the limitations of native File objects, you can preload files into a storage backend immediately upon selection. In this pattern, the form value only stores a lightweight file identifier (ID) instead of the file data itself.

    Advantages:

    • Smaller payloads (no Base64 overhead).
    • Files can be validated or processed on the server before the final form submission.
    • Files can be provided from the server.
  9. Handle undefined values in cfworker validator

    main

    Because the cfworker validator does not natively support undefined, you can use a valueToJSON function to normalize values. This implementation converts undefined or null to null and performs a deep clone of objects to ensure JSON compatibility.

    const valueToJSON = (v: FormValue) =>
      v === undefined || v === null
        ? null
        : typeof v === "object"
          ? JSON.parse(JSON.stringify(v))
          : v;