Skip to content

Inconsistency between Zod and ArkType validators and TypeError #1005

Open
@FLchs

Description

@FLchs

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)
    }
  })

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions