Skip to content

toJsonSchema ​

Makes the JSON Schema (draft-07) of definitions for a builder's registries and extensions, for editors and other tools. It is in its own entry, @whatsaaaa/form-builder-schema/json-schema, as it needs the optional peer @valibot/to-json-schema.

ts
const schema = toJsonSchema(registries)

Parameters ​

Explanation ​

It knows every type, rule and form rule by name, with their props and params, the layout's nodes, and the keys of the extensions. It can't know what only validate() checks, like duplicate names, defaults and paths.

Returns ​

  • schema JsonSchema

JsonSchemaRegistries ​

  • JsonSchemaRegistries
    • fieldTypes Readonly<Record<string, FieldTypeDef>>
    • rules Readonly<Record<string, RuleDef>>
    • formRules Readonly<Record<string, FormRuleDef>>
    • extensions readonly SchemaExtension[]: optional

Example ​

ts
import {
  createSchemaBuilder,
  defineFieldType,
} from '@whatsaaaa/form-builder-schema'
import { toJsonSchema } from '@whatsaaaa/form-builder-schema/json-schema'
import * as v from 'valibot'

const money = defineFieldType({
  kind: 'number',
  props: v.object({ currency: v.picklist(['EUR', 'USD']) }),
  schema: () => v.number(),
  meta: { label: 'Money', description: 'An amount in one currency' },
})

const builder = createSchemaBuilder({ fieldTypes: { money } })
const schema = toJsonSchema(builder)

// Write it next to your definitions, e.g. with Node's writeFileSync.
const file = JSON.stringify(schema, null, 2)

Guides ​