@nexdk/hooks
useResourceForm builds form state, dirty tracking and server-error mapping from the Zod schema of a resource. There is no second form schema to maintain.
Install
Section titled “Install”Not published to npm yet. Build it from the repository (packages/hooks). It peer-depends on nexdk, vue and zod (3 or 4).
Once published, install the package with everything it needs:
pnpm add nexdk vue zod @nexdk/hooksimport { defineResource } from 'nexdk'import { useResourceForm } from '@nexdk/hooks'import { z } from 'zod'
const product = defineResource({ name: 'products', schema: z.object({ name: z.string().min(1), price: z.number().default(0) }),})
const { values, errors, isDirty, handleSubmit, setServerErrors, fill, reset } = useResourceForm(product)
const onSubmit = handleSubmit(async (data) => { const { error } = await sdk.lomkit.products.mutate({ operation: 'create', attributes: data }) if (error?.status === 422) setServerErrors(error.errors as Record<string, string[]>)})The resource schema must be a Zod object schema (z.object): useResourceForm needs its shape and its safeParse.
values: aRefof the current values. Fields start at their Zod default, ornull.errors: aRefof field to messages, filled byhandleSubmitwhen validation fails.isDirty: aComputedRef<boolean>, true when a field differs from the baseline (the initial values, or whatfilllast set).handleSubmit(onValid): returns a submit handler. It validates, setserrorsand stops on failure, otherwise callsonValidwith the parsed data.setServerErrors(serverErrors): replaceserrorswith backend errors, for example theerrorsmap of a lomkit 422.fill(data): patchesvaluesand resets the dirty baseline.reset(): restores the initial values and clearserrors.
Supported schemas
Section titled “Supported schemas”useResourceForm is the one Zod-specific package. It supports Zod 3 (^3.24) and Zod 4 object schemas, and nexdk itself accepts any Standard Schema validator.
- Initial values are read by duck typing, not with
instanceof: a field with_def.typeName === 'ZodDefault'(Zod 3) or_zod.def.type === 'default'(Zod 4, includingzod/v4in Zod 3.25) starts at its default, which can be a value or a function. Any other field starts atnull. - Because nothing is compared with
instanceof, a schema built by another copy ofzod(a duplicated install, a monorepo,zod/v4) behaves like any other. - Validation calls
schema.safeParse, and the types come from the Standard Schema output of the schema, sovaluesand the data given toonValidare typed for both Zod versions. - Defaults wrapped by another modifier (
.default(1).optional()) are not unwrapped: the field starts atnull. - Valibot and ArkType schemas work with
nexdk, the drivers anddefineResource, but not withuseResourceForm: they have noshapeand nosafeParse. Passing one throws aResourceFormSchemaErrorwhen the form is created, and the types reject it.