Skip to content
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
]
},
"dependencies": {
"zod": "^3.23.8"
"zod": "^4.0.17"
},
"devDependencies": {
"@swc/core": "^1.5.29",
Expand Down
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions src/routeHandlerBuilder.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { describe, expect, expectTypeOf, it } from 'vitest';
import { z } from 'zod';
import { z } from 'zod/v4';
Comment thread
Melvynx marked this conversation as resolved.

import { createZodRoute } from './createZodRoute';
import { MiddlewareFunction } from './types';

const paramsSchema = z.object({
id: z.string().uuid(),
id: z.uuid(),
});

const querySchema = z.object({
Expand Down Expand Up @@ -60,6 +60,7 @@ describe('query validation', () => {
it('should validate and handle valid query', async () => {
const GET = createZodRoute()
.params(paramsSchema)
.query(querySchema)
.handler((request, context) => {
expectTypeOf(context.query).toMatchTypeOf<z.infer<typeof querySchema>>();
const search = context.query.search;
Expand Down Expand Up @@ -93,6 +94,7 @@ describe('query validation', () => {
it('should validate and handle valid query when query is array', async () => {
const GET = createZodRoute()
.params(paramsSchema)
.query(querySchema)
.handler((request, context) => {
expectTypeOf(context.query).toMatchTypeOf<z.infer<typeof querySchema>>();
const status = context.query.status;
Expand Down
11 changes: 7 additions & 4 deletions src/routeHandlerBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// eslint-disable-next-line import/no-named-as-default
import z from 'zod';
import z from 'zod/v4';

import {
HandlerFunction,
Expand Down Expand Up @@ -40,7 +40,7 @@ export class RouteHandlerBuilder<
};
readonly middlewares: Array<MiddlewareFunction<TContext, Record<string, unknown>, z.infer<TMetadata>>>;
readonly handleServerError?: HandlerServerErrorFn;
readonly metadataValue: z.infer<TMetadata>;
readonly metadataValue?: z.infer<TMetadata>;
readonly contextType!: TContext;

constructor({
Expand Down Expand Up @@ -116,8 +116,11 @@ export class RouteHandlerBuilder<
*/
defineMetadata<T extends z.Schema>(schema: T) {
return new RouteHandlerBuilder<TParams, TQuery, TBody, TContext, T>({
...this,
config: { ...this.config, metadataSchema: schema },
middlewares: [],
handleServerError: this.handleServerError,
contextType: this.contextType,
metadataValue: undefined,
});
}

Expand Down Expand Up @@ -199,7 +202,7 @@ export class RouteHandlerBuilder<
JSON.stringify({ message: 'Invalid params', errors: paramsResult.error.issues }),
);
}
params = paramsResult.data;
params = paramsResult.data as Record<string, unknown>;
}

// Validate the query against the provided schema
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/ban-types */

/* eslint-disable @typescript-eslint/no-explicit-any */
import { Schema } from 'zod';
import { Schema } from 'zod/v4';

/**
* Function that is called when the route handler is executed and all the middleware has been executed
Expand Down