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

Code before:
        } catch {
          console.error('Error: Malformed JSON in request body')
          return c.json(
            {
              success: false,
              message: 'Malformed JSON in request body',
            },
            400
          )
        }
        break
      case 'form':
        await (async () => {
          const contentType = c.req.header('Content-Type')
          if (contentType) {
            const arrayBuffer = c.req.bodyCache.arrayBuffer ?? (await c.req.raw.arrayBuffer())
            const formData = arrayBufferToFormData(arrayBuffer, contentType)
            const form: BodyData = {}
            formData.forEach((value, key) => {
              form[key] = value
            })
            value = form
            c.req.bodyCache.formData = formData
            c.req.bodyCache.arrayBuffer = arrayBuffer
          }
        })()
        break
      case 'query':
        value = Object.fromEntries(
          Object.entries(c.req.queries()).map(([k, v]) => {
            return v.length === 1 ? [k, v[0]] : [k, v]
          })
        )
        break
      case 'queries':
        value = c.req.queries()
        console.log('Warnings: Validate type `queries` is deprecated. Use `query` instead.')
        break
      case 'param':
        value = c.req.param() as Record<string, string>
        break
      case 'header':
        value = c.req.header()
        break
      case 'cookie':
        value = getCookie(c)
        break
    }

    const res = await validationFunc(value as never, c as never)


Review comment (performance):
I think we dont need to create a new `async function` in here, we can just change the code into something like this

```ts
   case 'form': {
      value = await c.req.parseBody();
      const contentType = c.req.header('Content-Type');

      // Just skip if no content type

      if (!contentType) break;
      const arrayBuffer =
        c.req.bodyCache.arrayBuffer ?? (await c.req.raw.arrayBuffer());
      const formData = arrayBufferToFormData(arrayBuffer, contentType);
      const form: BodyData = {};
      formData.forEach((value, key) => {
        form[key] = value;
      });
      value = form;
      c.req.bodyCache.formData = formData;
      c.req.bodyCache.arrayBuffer = arrayBuffer;

      break;
    }
```



Code after:
          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': {
        const contentType = c.req.header('Content-Type')
        if (contentType) {
          const arrayBuffer = c.req.bodyCache.arrayBuffer ?? (await c.req.raw.arrayBuffer())
          const formData = bufferToFormData(arrayBuffer, contentType)
          const form: BodyData = {}
          formData.forEach((value, key) => {
            form[key] = value
          })
          value = form
          c.req.bodyCache.formData = formData
          c.req.bodyCache.arrayBuffer = arrayBuffer
        }
        break
      }
      case 'query':
        value = Object.fromEntries(
          Object.entries(c.req.queries()).map(([k, v]) => {
            return v.length === 1 ? [k, v[0]] : [k, v]
          })
        )
        break
      case 'queries':
        value = c.req.queries()
        console.log('Warnings: Validate type `queries` is deprecated. Use `query` instead.')
        break
      case 'param':
        value = c.req.param() as Record<string, string>
        break
      case 'header':
        value = c.req.header()
        break
      case 'cookie':
        value = getCookie(c)
        break
    }

    const res = await validationFunc(value as never, c as never)