Skip to content

Commit

Permalink
Merge pull request #450 from kitbagjs/fix-zod-params
Browse files Browse the repository at this point in the history
Make sure zod isn't required if zod params are not being used
  • Loading branch information
pleek91 authored Jan 30, 2025
2 parents df267ed + e0cc308 commit e628e26
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 66 deletions.
5 changes: 4 additions & 1 deletion src/services/ zod.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { safeGetParamValue, safeSetParamValue } from './params'
import { z } from 'zod'
import { test, expect } from 'vitest'
import { initZod } from './zod'

enum Fruits {
Apple = 0,
Expand Down Expand Up @@ -40,7 +41,9 @@ test.each([
{ schema: z.record(z.string(), z.object({ foo: z.string() })), string: '{"one":{"foo":"bar"}}', parsed: { one: { foo: 'bar' } } },
{ schema: z.map(z.string(), z.number()), string: '[["one",1]]', parsed: new Map([['one', 1]]) },
{ schema: z.set(z.number()), string: '[1,2,3]', parsed: new Set([1, 2, 3]) },
])('given $schema, returns $parsed for $string', ({ schema, string, parsed }) => {
])('given $schema, returns $parsed for $string', async ({ schema, string, parsed }) => {
await initZod()

if (typeof parsed === 'string' || typeof parsed === 'number' || typeof parsed === 'boolean') {
expect(safeGetParamValue(string, schema)).toBe(parsed)
expect(safeSetParamValue(parsed, schema)).toBe(string)
Expand Down
7 changes: 7 additions & 0 deletions src/services/createRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { EmptyRouterPlugin, RouterPlugin } from '@/types/routerPlugin'
import { getRoutesForRouter } from './getRoutesForRouter'
import { getGlobalHooksForRouter } from './getGlobalHooksForRouter'
import { componentsStoreKey, createComponentsStore } from './createComponentsStore'
import { initZod, zotParamsDetected } from './zod'

type RouterUpdateOptions = {
replace?: boolean,
Expand Down Expand Up @@ -291,6 +292,12 @@ export function createRouter<
return initialize
}

const shouldInitZod = zotParamsDetected(routes)

if (shouldInitZod) {
await initZod()
}

initializing = true

await set(initialUrl, { replace: true, state: initialState })
Expand Down
7 changes: 3 additions & 4 deletions src/services/params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import { isParamWithDefault } from '@/services/withDefault'
import { ExtractParamType, isLiteralParam, isParamGetSet, isParamGetter } from '@/types/params'
import { LiteralParam, Param, ParamExtras, ParamGetSet } from '@/types/paramTypes'
import { stringHasValue } from '@/utilities/guards'
import { ZodSchema } from 'zod'
import { createZodParam } from './zod'
import { createZodParam, isZodParam } from './zod'

export function getParam(params: Record<string, Param | undefined>, paramName: string): Param {
return params[paramName] ?? String
Expand Down Expand Up @@ -184,7 +183,7 @@ export function getParamValue<T extends Param>(value: string | undefined, param:
throw new InvalidRouteParamValueError()
}

if (param instanceof ZodSchema) {
if (isZodParam(param)) {
return createZodParam(param).get(value, extras)
}

Expand Down Expand Up @@ -250,7 +249,7 @@ export function setParamValue(value: unknown, param: Param, isOptional = false):
return (value as LiteralParam).toString()
}

if (param instanceof ZodSchema) {
if (isZodParam(param)) {
return createZodParam(param).set(value, extras)
}

Expand Down
Loading

0 comments on commit e628e26

Please sign in to comment.