Repository: honojs/hono
File: src/validator/validator.ts (TypeScript)

Code before:
  V extends {
    in: { [K in U]: unknown extends InputType ? OutputType : InputType }
    out: { [K in U]: OutputType }
  } = {
    in: { [K in U]: unknown extends InputType ? OutputType : InputType }
    out: { [K in U]: OutputType }
  },
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  E extends Env = any
>(
  target: U,
  validationFunc: ValidationFunction<
    unknown extends InputType ? ValidationTargets[U] : InputType,
    OutputType,
    E,
    P2
  >
): MiddlewareHandler<E, P, V> => {
  return async (c, next) => {
    let value = {}

    switch (target) {
      case 'json':
        try {
          const contentType = c.req.header('Content-Type')
          if (!contentType || !/application\/json/.test(contentType)) {
            throw new Error(`Invalid HTTP header: Content-Type=${contentType}`)
          }
          /**
           * Get the arrayBuffer first, create JSON object via Response,
           * and cache the arrayBuffer in the c.req.bodyCache.
           */
          const arrayBuffer = c.req.bodyCache.arrayBuffer ?? (await c.req.raw.arrayBuffer())
          value = await new Response(arrayBuffer).json()
          c.req.bodyCache.json = value
          c.req.bodyCache.arrayBuffer = arrayBuffer
        } catch {
          console.error('Error: Malformed JSON in request body')
          return c.json(
            {
              success: false,
              message: 'Malformed JSON in request body',
            },
            400
          )
        }
        break
      case 'form': {
        try {
          const contentType = c.req.header('Content-Type')
          if (contentType) {

Review comment (suggestion):
`/^application\/json/` is beter.

```ts
if (!contentType || !/^application\/json/.test(contentType)) {
```

Code after:
  V extends {
    in: { [K in U]: unknown extends InputType ? OutputType : InputType }
    out: { [K in U]: OutputType }
  } = {
    in: { [K in U]: unknown extends InputType ? OutputType : InputType }
    out: { [K in U]: OutputType }
  },
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  E extends Env = any
>(
  target: U,
  validationFunc: ValidationFunction<
    unknown extends InputType ? ValidationTargets[U] : InputType,
    OutputType,
    E,
    P2
  >
): MiddlewareHandler<E, P, V> => {
  return async (c, next) => {
    let value = {}

    switch (target) {
      case 'json':
        try {
          const contentType = c.req.header('Content-Type')
          if (!contentType || !contentType.startsWith('application/json')) {
            throw new Error(`Invalid HTTP header: Content-Type=${contentType}`)
          }
          /**
           * Get the arrayBuffer first, create JSON object via Response,
           * and cache the arrayBuffer in the c.req.bodyCache.
           */
          const arrayBuffer = c.req.bodyCache.arrayBuffer ?? (await c.req.raw.arrayBuffer())
          value = await new Response(arrayBuffer).json()
          c.req.bodyCache.json = value
          c.req.bodyCache.arrayBuffer = arrayBuffer
        } catch {
          console.error('Error: Malformed JSON in request body')
          return c.json(
            {
              success: false,
              message: 'Malformed JSON in request body',
            },
            400
          )
        }
        break
      case 'form': {
        try {
          const contentType = c.req.header('Content-Type')
          if (contentType) {