Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experimental zod support for param types #446

Merged
merged 7 commits into from
Jan 29, 2025
Merged

Experimental zod support for param types #446

merged 7 commits into from
Jan 29, 2025

Conversation

pleek91
Copy link
Contributor

@pleek91 pleek91 commented Jan 27, 2025

Description

Since zod is a popular typescript schema validation library I wanted to see if we could support zod schemas as param types without any special public utilities or api. I believe this PR adds support for most zod schemas to be used as param types out of the box. Now any ZodSchema is a valid param type and the correct param type will be inferred from the ZodSchema and used for the param itself.

There are some schemas we cannot functionally support though. Most of which don't even make sense in a param context.
Specifically:

  • ZodPromise
  • ZodFunction
  • ZodIntersection
    I believe we could support ZodIntersection but its a bit more complex then the others so I want to see if there's some demand for it first.

These schemas are supported:

  • ZodString
  • ZodBoolean
  • ZodDate
  • ZodNumber
  • ZodLiteral
  • ZodObject
  • ZodEnum
  • ZodNativeEnum
  • ZodArray
  • ZodTuple
  • ZodUnion
  • ZodDiscriminatedUnion
  • ZodRecord
  • ZodMap
  • ZodSet

This means you can do things like this

const route = createRoute({
  path: path('/[myParam], {
    myParam: z.string()
  }
})

And more complex params like

const route = createRoute({
  path: path('/[myParam], {
    myParam: z.object({
      foo: z.string(),
      bar: z.number(),
      baz: z.date()
  }
})

All support is experimental and subject to change or removal. And there are likely many bugs and edge cases (though I'm optimistic there are not many).

Copy link

netlify bot commented Jan 27, 2025

Deploy Preview for kitbag-router ready!

Name Link
🔨 Latest commit 3f0e5fc
🔍 Latest deploy log https://app.netlify.com/sites/kitbag-router/deploys/67999a82f16d860008b741c6
😎 Deploy Preview https://deploy-preview-446--kitbag-router.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

src/services/zod.ts Outdated Show resolved Hide resolved
@pleek91 pleek91 merged commit edae1af into main Jan 29, 2025
6 checks passed
@pleek91 pleek91 deleted the zod-support branch January 29, 2025 03:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants