Skip to content

Commit

Permalink
fix(tbValidator): Fix tbValidator not calling hook when there's a fai…
Browse files Browse the repository at this point in the history
…l case (#742)

* Fix tbValidator not calling hook when there's a fail case

* add tests

* Add changeset
  • Loading branch information
Micnubinub authored Sep 24, 2024
1 parent 58c8e23 commit 39cb252
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/itchy-spiders-explain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hono/typebox-validator': patch
---

Fix fail case not returning error array in hook
11 changes: 10 additions & 1 deletion packages/typebox-validator/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ export function tbValidator<
}
return data
}
return c.json({ success: false, errors: [...Value.Errors(schema, data)] }, 400)

const errors = Array.from(Value.Errors(schema, data));
if (hook) {
const hookResult = hook({ success: false, errors }, c);
if (hookResult instanceof Response || hookResult instanceof Promise) {
return hookResult;
}
}

return c.json({ success: false, errors }, 400);
})
}
50 changes: 49 additions & 1 deletion packages/typebox-validator/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Type as T } from '@sinclair/typebox'
import { Type as T, TypeBoxError } from '@sinclair/typebox';
import { Hono } from 'hono'
import type { Equal, Expect } from 'hono/utils/types'
import { tbValidator } from '../src'
import { ValueError } from '@sinclair/typebox/value';

// eslint-disable-next-line @typescript-eslint/no-unused-vars
type ExtractSchema<T> = T extends Hono<infer _, infer S> ? S : never
Expand Down Expand Up @@ -106,6 +107,18 @@ describe('With Hook', () => {
message: `${data.id} is ${data.title}`,
})
}
).post(
'/errorTest',
tbValidator('json', schema, (result, c) => {
return c.json(result, 400)
}),
(c) => {
const data = c.req.valid('json')
return c.json({
success: true,
message: `${data.id} is ${data.title}`,
})
}
)

it('Should return 200 response', async () => {
Expand Down Expand Up @@ -140,4 +153,39 @@ describe('With Hook', () => {
expect(res).not.toBeNull()
expect(res.status).toBe(400)
})

it('Should return 400 response and error array', async () => {
const req = new Request('http://localhost/errorTest', {
body: JSON.stringify({
id: 123,
}),
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
})
const res = await app.request(req)
expect(res).not.toBeNull()
expect(res.status).toBe(400)

const {errors, success} = (await res.json()) as { success: boolean; errors: any[] }
expect(success).toBe(false)
expect(Array.isArray(errors)).toBe(true)
expect(errors.map((e: ValueError) => ({
'type': e?.schema?.type,
path: e?.path,
message: e?.message
}))).toEqual([
{
"type": "string",
"path": "/title",
"message": "Required property"
},
{
"type": "string",
"path": "/title",
"message": "Expected string"
}
])
})
})

0 comments on commit 39cb252

Please sign in to comment.