Skip to content
Myr

fix(validator): show "type" error messages if does not have "value" errors (#612)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Yusuke Wada · committed by GitHub · parent 0dc90e4 · Browse files · 3 files+43 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+43 added−0 removed

  • tests:+23 added−0 removed
  • other:+20 added−0 removed

3 files+43 added−0 removed

modified:deno_dist/middleware/validator/middleware.ts+10 added
modified:src/middleware/validator/middleware.test.ts+23 added
705 unmodified lines
706 const res = await app.request(req)707 expect(res.status).toBe(400)708 })709})710711describe('Special case', () => {712 const app = new Hono()713 app.get(714 '/search',715 validator((v) => ({716 q: v.query('q').isRequired(),717 page: v.query('page').asNumber(),718 })),719 (c) => {720 return c.text('Valid')721 }722 )723 it('Should show the type message error if it does not have "value" errors', async () => {724 const res = await app.request('http://localhost/search?q=foo')725 expect(res.status).toBe(400)726 expect(await res.text()).toBe(727 [728 'Invalid Value [undefined]: the query parameter "page" is invalid - should be "number"',729 ].join('\n')730 )731 })732})
modified:src/middleware/validator/middleware.ts+10 added
117 unmodified lines
118 }119 resultSet.results.push(result)120 }121122 // If it's invalid but it has no "value" messages, have to set the "type" messages.123 // This approach is verbose, but if do not so, the response body will be empty.124 if (!isValid && resultSet.messages.length === 0) {125 resultSet.results.map((r) => {126 if (!r.isValid && r.ruleType === 'type' && r.message) {127 resultSet.messages.push(r.message)128 }129 })130 }131132 // Set data on request object133 if (isValid) {134 // Set data on request object135 if (jsonData) {
48 unmodified lines