createSchemaBuilder
Creates a schema builder. It checks definitions, builds their Valibot schemas, and gives renderers the initial input, the layout and what is hidden.
const builder = createSchemaBuilder(options)Parameters
optionsSchemaBuilderOptions: optional
Explanation
Without options, the builder has every built-in kind, field type, rule and form rule. Your own entries merge over them by name, so an entry with a built-in name replaces it. includeDefaultRules: false leaves out the built-in rules and form rules, for registering only some.
The builder keeps the names of your entries in its type, so defineForm() knows every type, rule and form rule by name, and the keys your extensions add.
Mistakes in the registries, like an unknown kind, a reserved key in a type's props, or a built-in type whose rule you left out, throw a TypeError at once. Use one builder for everything a form does, so your own entries count everywhere.
Returns
builderSchemaBuilder
Example
import {
createSchemaBuilder,
type JsonForm,
} from '@whatsaaaa/form-builder-schema'
import * as v from 'valibot'
const definition: JsonForm = {
version: '1',
fields: [
{ name: 'email', type: 'email', label: 'Email', required: true },
{
name: 'password',
type: 'password',
label: 'Password',
required: true,
rules: [
{ rule: 'minLength', value: 10, message: 'At least 10 characters' },
],
},
],
}
const builder = createSchemaBuilder()
const schema = builder.build(definition)
const output = v.parse(schema, {
email: 'jane@example.com',
password: 'correct horse',
})
const result = v.safeParse(schema, { email: 'jane', password: 'short' }) // result.success is false
if (output.email !== 'jane@example.com' || result.success)
throw new Error('Unexpected result')