npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

zod-helper

v0.0.37

Published

Utility functions for working with Zod schemas

Readme

zod-helper

A powerful utility library for working with Zod schemas, focused on form generation, field manipulation, and schema transformation with full TypeScript support.

npm version license

Features

  • 🔄 Convert Zod schemas to JSON schema format
  • 📝 Generate type-safe form fields from Zod schemas
  • 🔢 Group and categorize form fields using various strategies
  • 📊 Organize fields by type, requirement, or custom groups
  • 🏗️ Support for nested objects and array structures
  • 🎯 Type-safe field position reordering
  • 🧩 Full TypeScript support with proper type inference
  • 🔌 Simple, chainable API

Installation

npm install zod-helper
# or
yarn add zod-helper
# or
pnpm add zod-helper

Quick Start

import { z } from 'zod';
import { ZodFormClassed } from 'zod-helper';

// Define your Zod schema
const userSchema = z.object({
  name: z.string().describe('Full name of the user'),
  email: z.string().email().describe('Email address'),
  age: z.number().min(18).describe('Age (must be 18+)'),
  role: z.enum(['admin', 'user', 'guest']).describe('User role'),
  preferences: z.object({
    theme: z.enum(['light', 'dark', 'system']).default('system'),
    notifications: z.boolean().default(true),
    language: z.string().default('en-US'),
  }).describe('User preferences'),
});

// Create a ZodFormClassed instance
const zodForm = new ZodFormClassed(userSchema);

// Rearrange field positions
zodForm.changeJsonFieldPositions({
  nested: {
    age: { position: 0 },
    name: { position: 1 },
    email: { position: 2 },
  },
});

// Group fields by keys with type safety
zodForm.groupByZodKeys(
  {
    'Personal Info': ['name', 'email', 'age'],
    'Account Settings': ['role'],
    'Preferences': [/^preferences/],
  },
  true // recursive grouping
);

// Access the processed fields
const groupedFields = zodForm.getGroupedFields;
const formFields = zodForm.getInputFields;
const jsonSchema = zodForm.getJsonSchema;

Core API

ZodFormClassed<T>

The main class for working with Zod schemas:

import { ZodFormClassed } from 'zod-helper';

const form = new ZodFormClassed(myZodSchema);

Methods

  • changeJsonFieldPositions(positionMeta): Reorder fields in the schema
  • groupByZodKeys(keys, recursive): Group fields with type-safe keys
  • getInputFields: Get the form fields generated from the schema
  • getJsonSchema: Get the JSON schema representation
  • getGroupedFields: Get the grouped form fields

Utility Functions

Schema Conversion

import { convertToJsonObject } from 'zod-helper';

const jsonSchema = convertToJsonObject(myZodSchema);

Form Field Generation

import { convertToFormsFields } from 'zod-helper';

const formFields = convertToFormsFields(jsonSchema);

Field Grouping

import { 
  groupByType, 
  groupByRequired,
  groupBySchemaKeys,
  createCustomGroups 
} from 'zod-helper';

// Group by field type (string, number, boolean, etc.)
const typeGroups = groupByType(formFields);

// Group by required/optional status
const requiredGroups = groupByRequired(formFields);

// Group by schema keys
const keyGroups = groupBySchemaKeys(formFields, {
  'Personal': ['name', 'email', 'phone'],
  'Professional': [/^job/, /^work/, 'office'],
});

// Create custom groups
const customGroups = createCustomGroups(formFields, [
  { name: 'Text Fields', pattern: /^name|email|description/ },
  { name: 'Numeric Fields', pattern: /^age|amount|count/ }
]);

Advanced Usage

Type-Safe Field Positions

import { ZodFormClassed } from 'zod-helper';

const userForm = new ZodFormClassed(userSchema);

// TypeScript will enforce valid field paths
userForm.changeJsonFieldPositions({
  nested: {
    // Fields at root level
    name: { position: 0 },
    email: { position: 1 },
    // Nested fields
    preferences: { 
      position: 2,
      nested: {
        theme: { position: 0 },
        notifications: { position: 1 },
      }
    }
  }
});

Working with Arrays and Objects

The library fully supports nested arrays and objects:

const productSchema = z.object({
  name: z.string(),
  variants: z.array(z.object({
    color: z.string(),
    size: z.string(),
    stock: z.number()
  }))
});

const productForm = new ZodFormClassed(productSchema);

// Group by keys with recursive processing
productForm.groupByZodKeys({
  'Product Info': ['name'],
  'Variant Details': ['variants']
}, true);

TypeScript Support

This library is built with full TypeScript support. You'll get:

  • Autocomplete for schema field names
  • Type checking for field grouping
  • Type-safe position adjustments
  • Proper type inference for nested structures

Use Cases

  • Generate form UI from Zod schemas
  • Create dynamic form builders
  • Transform Zod schemas for various backends
  • Build structured validation UIs
  • Organize form fields into logical groups

License

MIT