Form value type inference
json-schema-to-ts
Section titled “json-schema-to-ts”You can use json-schema-to-ts to infer types from JSON schema.
Installation
Section titled “Installation”npm i -D json-schema-to-tsyarn add -D json-schema-to-tspnpm add -D json-schema-to-tsbun add -d json-schema-to-tsimport { createForm, type Schema } from "@sjsf/form";import type { FromSchema } from "json-schema-to-ts";
import * as defaults from "@/components/form-defaults";
const schema = { type: "object", title: "Form title", properties: { text: { type: "string", title: "Text input", }, }, required: ["text"], additionalProperties: false} as const satisfies Schema;
const form = createForm({ ...defaults, schema, onSubmit: (value: FromSchema<typeof schema>) => { console.log(value) }})
// { text: string } | undefinedform.valueTypeBox
Section titled “TypeBox”TypeBox offers a unified type that can be statically checked by TypeScript and runtime asserted using standard Json Schema validation.
Installation
Section titled “Installation”npm i @sinclair/typeboxyarn add @sinclair/typeboxpnpm add @sinclair/typeboxbun add @sinclair/typeboximport { Type, type Static } from "@sinclair/typebox";import { createForm, type UiSchema } from "@sjsf/form";
import * as defaults from "@/components/form-defaults";
const schema = Type.Object({ text: Type.String(),});
const uiSchema: UiSchema = { "ui:options": { title: "Form title", }, text: { "ui:options": { title: "Text input", }, },};
const form = createForm({ ...defaults, schema, uiSchema, onSubmit: (value: Static<typeof schema>) => { console.log(value); },});
// { text: string } | undefinedform.value;