hono
hono copied to clipboard
Type instantiation is excessively deep and possibly infinite deno-ts(2589)
This issue might be due to a mismatch between hono
and zod-validator
. As a possible solution, you could try using the npm:
specifier as follows:
import { Hono } from 'npm:hono'
import { zValidator } from 'npm:@hono/zod-validator'
What I found is if I call zValidator with 'npm:@hono/zod-validator'
Deno retrive me the version 1.3 and the mismatch disappear, but the error message is different
But the latest version is 1.8, there is a reason Deno only support 1.3?
@yusukebe I don't find that getting both hono
and zod-validator
from npm makes any difference. Nor does getting zod-validator from esm.sh.
@hectorAguero I'm not finding that even version 0.1.3 of zod-validator avoids this error. 😢
This code will work correctly:
import { Hono } from 'npm:hono'
import { zValidator } from 'npm:@hono/zod-validator'
import { z } from 'npm:zod'
const app = new Hono()
const schema = z.object({
foo: z.string()
})
app.get('/', zValidator('query', schema), (c) => {
return c.json(c.req.valid('query'))
})
Deno.serve(app.fetch)
If errors still occur, try clearing the cache:
deno cache --reload hello.ts
Or you may need to specify the version as the latest version.
Just to say, I'm having the same problem with @hono/zod-openapi
the linter give me an error, but the code still run, if I switch to npm import the error is not showing
The error is No overload matches this call
Code sample is just the docs snippet with prettyJSON use, Both type of imports works in Deno Deploy.
import { OpenAPIHono, z, createRoute } from 'https://esm.sh/@hono/[email protected]';
// import { OpenAPIHono, z, createRoute } from 'npm:@hono/zod-openapi';
import { prettyJSON } from 'https://deno.land/x/[email protected]/middleware.ts';
//import { prettyJSON } from 'npm:hono/pretty-json';
const app = new OpenAPIHono();
app.use('/doc/*', prettyJSON())
const UserSchema = z
.object({
id: z.string().openapi({
example: '123',
}),
name: z.string().openapi({
example: 'John Doe',
}),
age: z.number().openapi({
example: 42,
}),
})
.openapi('User')
const sampleRoute = createRoute({
method: 'get',
path: '',
responses: {
200: {
content: {
'application/json': {
schema: UserSchema,
},
},
description: 'Retrieve user',
},
},
});
app.openapi(sampleRoute, async (c) => {
//fake await
const user = await Promise.resolve({
id: '123',
name: 'John Doe',
age: 42,
});
console.log(user)
const { id, name, age } = user;
return c.json({
id: id,
name: name,
age: age,
})
});
// The OpenAPI documentation will be available at /doc
app.doc('/doc', {
openapi: '3.0.0',
info: {
version: '1.0.0',
title: 'My API',
},
})
Deno.serve(app.fetch)
@fro-profesional
Mixing esm.sh
and deno.land
will result in a type error because each refers to a different hono
. This is unavoidable due to the limitations of TypeScript.
@yusukebe are we open to adding support for deno for zod-validator?
@jyotman
Zod Validator is supporting Deno.
My workaround in Deno:
- copy source code from
packages/zod-validator/src/index.ts
inhonojs/middleware
repo. - modify
import
as below:
from:
import type { Context, MiddlewareHandler, Env, ValidationTargets, TypedResponse, Input } from 'hono'
import { validator } from 'hono/validator'
import type { z, ZodSchema, ZodError } from 'zod'
to:
import type {
Env,
Input,
MiddlewareHandler,
TypedResponse,
ValidationTargets,
} from 'hono/types.ts';
import type { Context } from 'https://deno.land/x/[email protected]/context.ts';
import type { ZodSchema } from 'https://deno.land/x/[email protected]/types.ts';
import type { z, ZodError } from 'https://deno.land/x/[email protected]/mod.ts';
import { validator } from 'https://deno.land/x/[email protected]/validator/index.ts';
It is also necessary to use zod
by npm:
specifier
Yeah, making zod/hono external with esm.sh or using npm fixes the issue, thanks!
Related: https://github.com/StefanTerdell/zod-to-json-schema/issues/69#issuecomment-1572954056