Skip to content

Commit 6b7adee

Browse files
committed
Migrate from zod mini to zod
1 parent 2c9a9f8 commit 6b7adee

9 files changed

Lines changed: 34 additions & 37 deletions

File tree

readme.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# rouzer
22

3-
Type-safe routes shared by your server and client, powered by `zod/mini` (input validation + transforms), `@remix-run/route-pattern` (URL matching), and `alien-middleware` (typed middleware chaining). The router output is intended to be used with `@hattip/core` adapters.
3+
Type-safe routes shared by your server and client, powered by `zod` (input validation + transforms), `@remix-run/route-pattern` (URL matching), and `alien-middleware` (typed middleware chaining). The router output is intended to be used with `@hattip/core` adapters.
44

55
## Install
66

@@ -14,7 +14,7 @@ Everything is imported directly from `rouzer`.
1414

1515
```ts
1616
// routes.ts
17-
import * as z from 'zod/mini'
17+
import * as z from 'zod'
1818
import { $type, route } from 'rouzer'
1919

2020
export const helloRoute = route('hello/:name', {
@@ -171,6 +171,6 @@ const pingText = await pingResponse.text()
171171

172172
## Add an endpoint
173173

174-
1. Declare it in `routes.ts` with `route(…)` and `zod/mini` schemas.
174+
1. Declare it in `routes.ts` with `route(…)` and `zod` schemas.
175175
2. Implement the handler in your router assembly with `createRouter(…).use(routes, { … })`.
176176
3. Call it from the client with the generated helper via `client.json` or `client.request`.

src/server/router.ts

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
MiddlewareTypes,
99
RequestContext,
1010
} from 'alien-middleware'
11-
import * as z from 'zod/mini'
11+
import * as z from 'zod'
1212
import { mapValues } from '../common.js'
1313
import type { Routes, RouteSchemaMap } from '../types.js'
1414
import type { RouteRequestHandlerMap } from './types.js'
@@ -276,7 +276,7 @@ function httpClientError(
276276

277277
function parsePathParams(
278278
context: AdapterRequestContext & { path?: {} },
279-
schema: z.ZodMiniType<any, any>,
279+
schema: z.ZodType<any, any>,
280280
params: {}
281281
) {
282282
const result = schema.safeParse(params)
@@ -289,7 +289,7 @@ function parsePathParams(
289289

290290
function parseHeaders(
291291
context: AdapterRequestContext & { headers?: {} },
292-
schema: z.ZodMiniType<any, any>
292+
schema: z.ZodType<any, any>
293293
) {
294294
const headers = Object.fromEntries(context.request.headers as any)
295295
const result = schema.safeParse(headers)
@@ -302,7 +302,7 @@ function parseHeaders(
302302

303303
function parseQueryString(
304304
context: AdapterRequestContext & { url?: URL; query?: {} },
305-
schema: z.ZodMiniType<any, any>
305+
schema: z.ZodType<any, any>
306306
) {
307307
const result = schema.safeParse(
308308
Object.fromEntries(context.url!.searchParams as any)
@@ -316,7 +316,7 @@ function parseQueryString(
316316

317317
async function parseRequestBody(
318318
context: AdapterRequestContext & { body?: {} },
319-
schema: z.ZodMiniType<any, any>
319+
schema: z.ZodType<any, any>
320320
) {
321321
const result = await context.request.json().then(
322322
body => schema.safeParse(body),
@@ -329,39 +329,36 @@ async function parseRequestBody(
329329
return null
330330
}
331331

332-
const seen = new WeakMap<z.ZodMiniType<any, any>, z.ZodMiniType<any, any>>()
332+
const seen = new WeakMap<z.ZodType<any, any>, z.ZodType<any, any>>()
333333

334334
/**
335335
* Traverse object and array schemas, finding schemas that expect a number or
336336
* boolean, and replace those schemas with a new schema that parses the input
337337
* value as a number or boolean.
338338
*/
339-
function enableStringParsing(schema: z.ZodMiniType): typeof schema {
339+
function enableStringParsing(schema: z.ZodType): typeof schema {
340340
if (schema.type === 'optional') {
341-
const { def } = schema as z.ZodMiniOptional<z.ZodMiniType>
341+
const { def } = schema as z.ZodOptional<z.ZodType>
342342
return z.optional(enableStringParsing(def.innerType))
343343
}
344344
if (schema.type === 'number') {
345-
return z.pipe(z.transform(Number), schema as z.ZodMiniNumber<number>)
345+
return z.pipe(z.transform(Number), schema as z.ZodNumber)
346346
}
347347
if (schema.type === 'boolean') {
348-
return z.pipe(
349-
z.transform(toBooleanStrict),
350-
schema as z.ZodMiniBoolean<boolean>
351-
)
348+
return z.pipe(z.transform(toBooleanStrict), schema as z.ZodBoolean)
352349
}
353350
if (schema.type === 'object') {
354351
const cachedSchema = seen.get(schema)
355352
if (cachedSchema) {
356353
return cachedSchema
357354
}
358-
const { def } = schema as z.ZodMiniObject<Record<string, z.ZodMiniType>>
355+
const { def } = schema as z.ZodObject<Record<string, z.ZodType>>
359356
const newSchema = z.object(mapValues(def.shape, enableStringParsing))
360357
seen.set(schema, newSchema)
361358
return newSchema
362359
}
363360
if (schema.type === 'array') {
364-
const { def } = schema as z.ZodMiniArray<z.ZodMiniType>
361+
const { def } = schema as z.ZodArray<z.ZodType>
365362
return z.array(enableStringParsing(def.element))
366363
}
367364
return schema

src/server/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type {
44
MiddlewareChain,
55
MiddlewareContext,
66
} from 'alien-middleware'
7-
import type * as z from 'zod/mini'
7+
import type * as z from 'zod'
88
import { Promisable } from '../common.js'
99
import type { InferRouteResponse, Routes, RouteSchema } from '../types.js'
1010

src/types.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
import { Params, RoutePattern } from '@remix-run/route-pattern'
2-
import * as z from 'zod/mini'
2+
import * as z from 'zod'
33
import { Unchecked } from './common.js'
44

55
export type { Unchecked }
66

77
export type QueryRouteSchema = {
8-
path?: z.ZodMiniObject<any>
9-
query?: z.ZodMiniObject<any>
8+
path?: z.ZodObject<any>
9+
query?: z.ZodObject<any>
1010
body?: never
11-
headers?: z.ZodMiniObject<any>
11+
headers?: z.ZodObject<any>
1212
response?: Unchecked<any>
1313
}
1414

1515
export type MutationRouteSchema = {
16-
path?: z.ZodMiniObject<any>
16+
path?: z.ZodObject<any>
1717
query?: never
18-
body?: z.ZodMiniType<any, any>
19-
headers?: z.ZodMiniObject<any>
18+
body?: z.ZodType<any, any>
19+
headers?: z.ZodObject<any>
2020
response?: Unchecked<any>
2121
}
2222

@@ -27,10 +27,10 @@ export type RouteSchemaMap = {
2727
PATCH?: MutationRouteSchema
2828
DELETE?: MutationRouteSchema
2929
ALL?: {
30-
path?: z.ZodMiniObject<any>
31-
query?: z.ZodMiniObject<any>
30+
path?: z.ZodObject<any>
31+
query?: z.ZodObject<any>
3232
body?: never
33-
headers?: z.ZodMiniObject<any>
33+
headers?: z.ZodObject<any>
3434
response?: never
3535
}
3636
}

test/fixtures/client-validation/routes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { route } from 'rouzer'
2-
import * as z from 'zod/mini'
2+
import * as z from 'zod'
33

44
export const queryRoute = route('query', {
55
GET: {

test/fixtures/client-validation/test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export default {
1313

1414
await expect(queryClient.queryRoute.GET({ query: { q: 'x' } })).rejects
1515
.toMatchInlineSnapshot(`
16-
[$ZodError: [
16+
[ZodError: [
1717
{
1818
"origin": "string",
1919
"code": "too_small",
@@ -22,7 +22,7 @@ export default {
2222
"path": [
2323
"q"
2424
],
25-
"message": "Invalid input"
25+
"message": "Too small: expected string to have >=2 characters"
2626
}
2727
]]
2828
`)
@@ -37,7 +37,7 @@ export default {
3737

3838
await expect(bodyClient.bodyRoute.POST({ body: { count: -1 } })).rejects
3939
.toMatchInlineSnapshot(`
40-
[$ZodError: [
40+
[ZodError: [
4141
{
4242
"origin": "number",
4343
"code": "too_small",
@@ -46,7 +46,7 @@ export default {
4646
"path": [
4747
"count"
4848
],
49-
"message": "Invalid input"
49+
"message": "Too small: expected number to be >0"
5050
}
5151
]]
5252
`)

test/fixtures/coercion/routes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { $type, route } from 'rouzer'
2-
import * as z from 'zod/mini'
2+
import * as z from 'zod'
33

44
export const coercionRoute = route('coercion/:id', {
55
GET: {

test/fixtures/server-validation/routes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { $type, route } from 'rouzer'
2-
import * as z from 'zod/mini'
2+
import * as z from 'zod'
33

44
export const validateRoute = route('validate/:id', {
55
GET: {

test/infer-route-body.test-d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as z from 'zod/mini'
1+
import * as z from 'zod'
22
import type { InferRouteBody, InferRouteMethodBody } from 'rouzer'
33
import { route } from 'rouzer'
44

0 commit comments

Comments
 (0)