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

Inconsistency between Zod and ArkType validators and TypeError #1005

Open
FLchs opened this issue Mar 5, 2025 · 0 comments · May be fixed by #1006
Open

Inconsistency between Zod and ArkType validators and TypeError #1005

FLchs opened this issue Mar 5, 2025 · 0 comments · May be fixed by #1006
Labels

Comments

@FLchs
Copy link

FLchs commented Mar 5, 2025

Which middleware has the bug?

@hono/arktype-validator

What version of the middleware?

2.0.0

What version of Hono are you using?

4.7.2

What runtime/platform is your app running on? (with version if possible)

bun 1.2.0

What steps can reproduce the bug?

repro repository: https://github.com/FLchs/bun-hono-arktype

import { Hono } from 'hono'
import { type } from 'arktype'
import { arktypeValidator } from '@hono/arktype-validator'

import { z } from 'zod'
import { zValidator } from '@hono/zod-validator'

const schema = type({
  name: 'string',
  age: 'number'
})

const schemaZ = z.object({
  name: z.string(),
  age: z.number()
})

const app = new Hono()


app.post('/ark', arktypeValidator('json', schema), (c) => {
  const data = c.req.valid('json')
  return c.json({
    success: true,
    message: `${data.name} is ${data.age}`,
  })
})

app.post(
  '/zod',
  zValidator('json', schemaZ), (c) => {
    const data = c.req.valid('json')
    return c.json({
      success: true,
      message: `${data.name} is ${data.age}`,
    })
  })

export default app

Now call both enpoint with invalid body (age should be a number):

{
"name": "francois",
"age¨: "35"
}

What is the expected behavior?

Both endpoint should error in the same way : sending a 400

What do you see instead?

Zod endpoint fail successfully (!) by sending:

{
  "success": false,
  "error": {
    "issues": [
      {
        "code": "invalid_type",
        "expected": "number",
        "received": "string",
        "path": [
          "age"
        ],
        "message": "Expected number, received string"
      }
    ],
    "name": "ZodError"
  }
}

ArkType endpoint will crash the server:
Internal Server Error
logs:

221 |       return this.#newResponse(text, arg, headers);
222 |     }
223 |     return this.#newResponse(text, arg);
224 |   };
225 |   json = (object, arg, headers) => {
226 |     const body = JSON.stringify(object);
                            ^
TypeError: JSON.stringify cannot serialize cyclic structures.
      at json (/home/francois/Projects/Personal/bun-hono-arktype/node_modules/hono/dist/context.js:226:23)
      at <anonymous> (/home/francois/Projects/Personal/bun-hono-arktype/node_modules/hono/dist/validator/validator.js:76:23)

Additional information

Using the hook works:

  arktypeValidator('json', schema, (result, c) => {
    if (!result.success) {
      return c.text('Invalid!', 400)
    }
  })
@FLchs FLchs added the triage label Mar 5, 2025
@FLchs FLchs linked a pull request Mar 5, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant