1.5.5 • Published 1 year ago

zod-key-parser v1.5.5

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

⚠ 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 me@raflymaulana.me or just make a GitHub issue at this repository.

1.5.5

1 year ago

1.5.4

1 year ago

1.5.3

1 year ago

1.5.2

1 year ago

1.5.1

1 year ago

1.5.0

2 years ago

1.5.0-canary.0

2 years ago

1.4.1

2 years ago

1.4.0

2 years ago

1.3.2

2 years ago

1.3.1

2 years ago

1.3.0

2 years ago

1.2.2

2 years ago

1.2.1

2 years ago

1.2.0

2 years ago

1.1.4

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago