Skip to content

API reference > @kontsedal/olas-zod > createZodForm

createZodForm() function ​

Walk a Zod schema and emit the equivalent Olas Form / FieldArray / Field tree, with validators auto-attached.

  • z.object(...) → Form - z.array(...) → FieldArray (recurses on the element) - leaf schemas → Field with zodValidator(...) attached

Each leaf's initial value is the Zod default if present, otherwise an empty value for that type ('' for strings, 0 for numbers, etc.). A.default(...) on an object or an array seeds that nested form or field array as a whole, as schema.parse({}) fills the key.

A rule on an object or an array is enforced too. A root .refine(fn) lands in form.topLevelErrors, a .refine(fn, { path: ['confirm'] }) lands on confirm, and z.array(...).min(3) lands in that array'stopLevelErrors. The form parses the whole schema for these on every change, so a schema with no such rule does not get that validator.

The return type is structurally precise — form.fields.title.value isstring (not string | boolean | …), form.fields.subtasks.add(...) accepts the exact item shape, etc. Consumers do not need to hand-write a CardForm = Form<{...}> matching the schema.

Signature:

typescript
export declare function createZodForm<T extends z.ZodObject<z.ZodRawShape>>(ctx: Ctx, schema: T, options?: ZodFormOptions<T>): Form<{ [K in keyof T['shape']]: ZodToLeaf<T['shape'][K]>; }>;

Parameters ​

Parameter

Type

Description

ctx

Ctx

schema

T

options

ZodFormOptions<T>

(Optional)

Returns:

Form<{ [K in keyof T['shape']]: ZodToLeaf<T['shape'][K]>; }>

Released under the MIT License.