Skip to content
toiroakrPublic

About

generate minimum data from zod schema.

Resources

Stars

67 stars

Watchers

1 watching

Forks

Repository files navigation

zod-empty

License: MIT npm version

Generate minimum data from zod schema.

Same motivation as json-schema-empty

All libraries that generate data from a json-schema I could find generate random data that conforms to a json schema. This is nice for testing but is not well-suited for generating default data for forms for example.

If you’re using Zod 3, please switch to zod-empty@1.

Usage

To install zod-empty, run:

npm install zod-empty

Use with react-hook-form like this: (ref. react-hook-form)

// Please check default value for schema here.

import { useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import { z } from "zod";
import { init /* , empty */ } from "zod-empty";

const schema = z.object({
  name: z.string().min(1, { message: "Required" }),
  age: z.number().min(10),
  hobbies: z.array(z.string()),
});
// create default values with zod-empty
const defaultValues = init(schema); // => { name: "", age: 10, hobbies: [] }
// or const defaultValues = empty(schema); // => { name: null, age: null, hobbies: [] }

const App = () => {
  const {
    register,
    handleSubmit,
    formState: { errors },
  } = useForm({
    // add default values
    defaultValues,
    resolver: zodResolver(schema),
  });

  return (
    <form onSubmit={handleSubmit((d) => console.log(d))}>
      <input {...register("name")} />
      {errors.name?.message && <p>{errors.name?.message}</p>}
      <input type="number" {...register("age", { valueAsNumber: true })} />
      {errors.age?.message && <p>{errors.age?.message}</p>}
      <input type="submit" />
    </form>
  );
};

Development

Use Node.js 22.18 or newer (Node.js 24.11 or newer on the 24.x line) for development and the pnpm version pinned in package.json. The published library continues to support Node.js 22.13 or newer.

pnpm install --frozen-lockfile
pnpm build
pnpm build:sample
pnpm check
pnpm test

pnpm check checks formatting with oxfmt and linting with oxlint. Use pnpm format to format files and pnpm lint:fix to apply safe lint fixes. The React Hook Form sample is included in the pnpm workspace and uses the local library build.

Dependency updates

Dependency updates use a seven-day release-age policy in both Renovate and pnpm. pnpm also checks transitive packages during installation. Minor and patch updates can merge automatically after CI succeeds; major updates and peer dependency changes require manual merging. Renovate performs the merge itself instead of delegating it to GitHub auto-merge.

The main branch requires the check, build, test (22.13.0), test (24.x), and all three peer-floor checks from GitHub Actions before merging. Keep these required check names in sync when changing the CI matrix.

About

generate minimum data from zod schema.

Resources

Stars

67 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages