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

Code before:
      const messages = [
        'Invalid Value [undefined]: the request header "id" is invalid - should be "number"',
      ]
      expect(results[0].message).toBe(messages.join('\n'))
    }

    const vObject = v.object('pager', (v) => ({
      prev: v.query('prev').asBoolean(),
      next: v.body('next').asBoolean(),
    }))

    const messages = [
      'Invalid Value [undefined]: the query parameter "prev" is invalid - should be "boolean"',
      'Invalid Value [undefined]: the request body "next" is invalid - should be "boolean"',
    ]

    for (const [i, validator] of vObject.getValidators().entries()) {
      const results = await validator.validate(req)
      expect(results[0].isValid).toBe(false)
      expect(results[0].message).toBe(`${messages[i]}`)
    }
  })

  it('Should threat `v.json()` as a nested array type only inside an array', async () => {
    const json = {
      blog: {
        totalPosts: '3',
        posts: [
          {
            id: 123,
            title: 'JavaScript',
          },
          {
            id: '456',
            title: 'Framework',
          },
          {
            id: 789,
            title: 'Hono',
          },
        ],
        highlightedPostId: '123',
      }
    }
  
    const req = new Request('http://localhost/', {
      method: 'POST',
      body: JSON.stringify(json),
    })

    const v = new Validator()

Review comment (security):
If we add the `author.name` such as below:

```ts
      blog: {
        totalPosts: '3',
        posts: [
          {
            id: 123,
            title: 'JavaScript',
            author: {
              name: 'foo',
            },
          },
          {
            id: '456',
            title: 'Framework',
            author: {
              name: 'bar',
            },
          },
          {
            id: 789,
            title: 'Hono',
            author: {
              name: 'baz',
            },
          },
        ],
        highlightedPostId: '123',
      },
```

and write the validator below:

```ts
    const v = new Validator()
    const vObject = v.object('blog', (v) => ({
      posts: v.array('posts', (v) => ({
        author: v.object('author', (v) => ({
          name: v.json('name'),
        })),
      })),
    }))
```

then, we expect it to be valid, but it will no be valid. So, I think we have to remove this line:

https://github.com/honojs/hono/blob/8ed7763c38e153ee78860724582ffd0f0d96a845/src/validator/validator.ts#L111


Code after:
      const messages = [
        'Invalid Value [undefined]: the request header "id" is invalid - should be "number"',
      ]
      expect(results[0].message).toBe(messages.join('\n'))
    }

    const vObject = v.object('pager', (v) => ({
      prev: v.query('prev').asBoolean(),
      next: v.body('next').asBoolean(),
    }))

    const messages = [
      'Invalid Value [undefined]: the query parameter "prev" is invalid - should be "boolean"',
      'Invalid Value [undefined]: the request body "next" is invalid - should be "boolean"',
    ]

    for (const [i, validator] of vObject.getValidators().entries()) {
      const results = await validator.validate(req)
      expect(results[0].isValid).toBe(false)
      expect(results[0].message).toBe(`${messages[i]}`)
    }
  })

  it('Should threat `v.json()` as a nested array type only inside an array', async () => {
    const json = {
      blog: {
        totalPosts: '3',
        posts: [
          {
            id: 123,
            title: 'JavaScript',
            author: {
              name: 'foo',
            },
          },
          {
            id: '456',
            title: 'Framework',
            author: {
              name: 'bar',
            },
          },
          {
            id: 789,
            title: 'Hono',
            author: {
              name: 'baz',
            },
          },
        ],
        highlightedPostId: '123',