BuzzForm
BuzzFormDocs

Schema

Learn how to define form schemas and infer TypeScript types from your field definitions.

Schema

A BuzzForm schema is the single source of truth for your form. It defines the fields, validation, and generates TypeScript types automatically.

Creating a Schema

Use createSchema() to define your form:

import { createSchema } from "@buildnbuzz/buzzform";

const schema = createSchema([
  { type: "text", name: "name", label: "Name", required: true },
  { type: "email", name: "email", label: "Email", required: true },
  { type: "number", name: "age", label: "Age", min: 18 },
]);

The schema contains:

  • fields - Your field definitions array
  • zodSchema - Auto-generated Zod validation schema

Type Inference

Use InferType to get full TypeScript types from your schema:

import { createSchema, InferType } from "@buildnbuzz/buzzform";

const schema = createSchema([
  { type: "text", name: "name", label: "Name", required: true },
  { type: "email", name: "email", label: "Email" },
  { type: "checkbox", name: "newsletter", label: "Subscribe" },
]);

type FormData = InferType<typeof schema>;
// Result: { name: string, email?: string, newsletter: boolean }

This is especially useful for server actions:

actions.ts
"use server";

import { InferType } from "@buildnbuzz/buzzform";
import { contactSchema } from "./schema";

type ContactData = InferType<typeof contactSchema>;

export async function submitContact(data: ContactData) {
  // data is fully typed!
  await db.contacts.create(data);
}

Field Structure

Every field has these common properties:

PropertyTypeDescription
typestringField type (text, email, select, etc.)
namestringKey in form data
labelstring | ReactNode | falseDisplay label
descriptionstring | ReactNodeHelp text below field
placeholderstringPlaceholder text
requiredbooleanWhether field is required
disabledboolean | functionDisable input
hiddenboolean | functionHide field
readOnlyboolean | functionRead-only mode
defaultValueanyInitial value
validatefunctionCustom validation
conditionfunctionConditional visibility
styleobjectStyling options

Nested Data Structures

Use group fields to create nested objects:

const schema = createSchema([
  { type: "text", name: "name", label: "Name" },
  {
    type: "group",
    name: "address",
    label: "Address",
    fields: [
      { type: "text", name: "street", label: "Street" },
      { type: "text", name: "city", label: "City" },
    ],
  },
]);

// Result: { name: string, address: { street: string, city: string } }

Array Data

Use array fields for repeatable items:

const schema = createSchema([
  {
    type: "array",
    name: "contacts",
    label: "Contacts",
    minRows: 1,
    maxRows: 5,
    fields: [
      { type: "text", name: "name", label: "Name" },
      { type: "email", name: "email", label: "Email" },
    ],
  },
]);

// Result: { contacts: Array<{ name: string, email: string }> }

On this page