Created
August 27, 2025 12:27
-
-
Save franky47/3908bf9510bcc6e4545df2773460e1f7 to your computer and use it in GitHub Desktop.
Using Zod codecs with nuqs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { createParser, useQueryState } from 'nuqs' | |
| import { z } from 'zod' | |
| export function createZodCodecParser< | |
| Input extends z.ZodCoercedString<string>, | |
| Output extends z.ZodType | |
| >( | |
| codec: z.ZodCodec<Input, Output>, | |
| eq: (a: z.output<Output>, b: z.output<Output>) => boolean = (a, b) => a === b | |
| ) { | |
| return createParser<z.output<Output>>({ | |
| parse(query) { | |
| return codec.parse(query) | |
| }, | |
| serialize(value) { | |
| return codec.encode(value) | |
| }, | |
| eq | |
| }) | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
From the video: https://www.youtube.com/watch?v=k4lWvklUxUE