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 🙏

© 2024 – Pkg Stats / Ryan Hefner

zod-key-parser

v1.5.5

Published

Parse zod schema into form keys or select format

Downloads

1,605

Readme

⚠ Please use latest version

All bugs related to peer dependency, fail to parsing, and other bugs has been fixed in latest version

If you still have any problem, please make an issue in the GitHub repository


What's New on v.1.5.5?

  • Add support for ZodDefault, ZodPromise, ZodReadonly, and ZodNaN
  • Add support for ZodEffects (refine, transform, etc)
  • BREAKING CHANGE: If you use customStringParser, the props is now an object instead of single string
  • Add more string parser options
  • Fix broken Date validation
  • Deprecating prismaKeys, use selectKeys instead

Zod Key Parser

Parse your Zod schema into keys or ORM select format

What This Do?

Transform this schema:

import { parseZodSchema } from "zod-key-parser";

const schema = z.object({
    a: z.boolean(),
    b: z.string(),
    c: z.string(),
    d: z.string(),
    e: z.string(),
    f: z.string(),
    g: z.string(),
    h: z.array(
        z.object({
            ha: z.number(),
            hb: z.string(),
            hc: z.boolean(),
            hd: z.string(),
        })
    ),
    i: z.object({
        ia: z.number(),
    }),
});

Into This

// schema.keys

{
  "a": "a",
  "b": "b",
  "c": "c",
  "d": "d",
  "e": "e",
  "f": "f",
  "g": "g",
  "h": (index: number) => {
  	"ha": `h.${index}.ha`,
  	"hb": `h.${index}.hb`,
  	"hc": `h.${index}.hc`,
  	"hd": `h.${index}.hd`,
  },
  "i": {
    "ia": "i.ia"
  }
}

So that you can use it on your form like this:


<input name={schema.keys.a} type="string" ... />

And This

// schema.selectKeys
{
  "a": true,
  "b": true,
  "c": true,
  "d": true,
  "e": true,
  "f": true,
  "g": true,
  "h": {
    "select": {
      "ha": true,
      "hb": true,
      "hc": true,
      "hd": true
    }
  },
  "i": {
    "select": {
      "ia": true
    }
  }
}

So that you can use it on your ORM like this

const something = await orm.table.findUnique({
  where: ...,
  select: schema.selectKeys
})

How About the Opposite? Don't Worry

There's 2 function, formatObject and formatFormData() that you can use

1. formatObject(object)

Use it to format object to keys format

  • Format this:
const inputData = {
    formkey1: "something",
    formkey2: "something",
    "formkey3.a": "something",
    "formkey3.b": "something",
    "formkey5.c": "something",
    "formarray.0.a": "something",
    "formarray.1.a": "something",
    "formarray.1.ab": "something",
    "formarray.2.c.d.0.a": "true",
    "formarray.2.c.d.1.a": "true",
};

const formattedData = formatObject(inputData);
  • Into this
// formattedData
{
  "formkey1": "something",
  "formkey2": "something",
  "formkey3": {
    "a": "something",
    "b": "something"
  },
  "formkey5": {
    "c": "something"
  },
  "formarray": [
    {
      "a": "something"
    },
    {
      "a": "something",
      "ab": "something"
    },
    {
      "c": {
        "d": [
          {
            "a": true
          },
          {
            "a": true
          }
        ]
      }
    }
  ]
}

2. formatFormData(formData)

Use it to format data from form action directly, especially for React/Next.js user who use server action

It also use formatObject() under the hood

const formAction = (formData: FormData) => {
  const parsed = schema.safeParse(formatFormData(formData));

  if (!parsed.success) {
    ...
  }
}

return (
  <form action={formAction}>
      <input name={schema.keys.a} />
      <input name={schema.keys.b} />
  </form>
)

What's The Point of This?

Pros

  • Avoid typos in form name
  • Easily parsed form data with a little lines of code
  • For ORM, improve your database performance by only selecting data to be used
  • You'll lovin it like McDonalds says

Cons

  • Idk, maybe it's just too much for you? let me know

What You Should Know

  • Currently it doesn't support non object schema, if your schema is just like z.string() it wont parse anything since it doesn't have a key
  • It Supports ZodEnum, ZodUnion, ZodIntersection, ZodArray, ZodOptional, ZodNullable, and ZodObject also of course the primitives type like string, boolean and so on, i don't know if there are any Zod class that i should be aware of since i myself doesn't use anything beside what I've specify before.

Any Suggestion or Problem?

Feel free to reach me at [email protected] or just make a GitHub issue at this repository.