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

Make sure zod isn't required if zod params are not being used #450

Merged
merged 1 commit into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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