Skip to content

Instantly share code, notes, and snippets.

@franky47
Created August 27, 2025 12:27
Show Gist options
  • Select an option

  • Save franky47/3908bf9510bcc6e4545df2773460e1f7 to your computer and use it in GitHub Desktop.

Select an option

Save franky47/3908bf9510bcc6e4545df2773460e1f7 to your computer and use it in GitHub Desktop.
Using Zod codecs with nuqs
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
})
}
@franky47
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment