import { Hono } from 'hono' import type { Equal, Expect } from 'hono/utils/types' import { number, object, string } from 'valibot' import { vValidator } from '../src' // eslint-disable-next-line @typescript-eslint/no-unused-vars type ExtractSchema = T extends Hono ? S : never describe('Basic', () => { const app = new Hono() const schema = object({ name: string(), age: number(), }) const route = app.post('/author', vValidator('json', schema), (c) => { const data = c.req.valid('json') return c.jsonT({ success: true, message: `${data.name} is ${data.age}`, }) }) type Actual = ExtractSchema type Expected = { '/author': { $post: { input: { json: { name: string age: number } } output: { success: boolean message: string } } } } // eslint-disable-next-line @typescript-eslint/no-unused-vars type verify = Expect> it('Should return 200 response', async () => { const req = new Request('http://localhost/author', { body: JSON.stringify({ name: 'Superman', age: 20, }), headers: { 'Content-Type': 'application/json', }, method: 'POST', }) const res = await app.request(req) expect(res).not.toBeNull() expect(res.status).toBe(200) expect(await res.json()).toEqual({ success: true, message: 'Superman is 20', }) }) it('Should return 400 response', async () => { const req = new Request('http://localhost/author', { body: JSON.stringify({ name: 'Superman', age: '20', }), headers: { 'Content-Type': 'application/json', }, method: 'POST', }) const res = await app.request(req) expect(res).not.toBeNull() expect(res.status).toBe(400) const data = (await res.json()) as { success: boolean } expect(data['success']).toBe(false) }) }) describe('With Hook', () => { const app = new Hono() const schema = object({ id: number(), title: string(), }) app.post( '/post', vValidator('json', schema, (result, c) => { if (!result.success) { return c.text('Invalid!', 400) } const data = result.output return c.text(`${data.id} is valid!`) }), (c) => { const data = c.req.valid('json') return c.json({ success: true, message: `${data.id} is ${data.title}`, }) } ) it('Should return 200 response', async () => { const req = new Request('http://localhost/post', { body: JSON.stringify({ id: 123, title: 'Hello', }), headers: { 'Content-Type': 'application/json', }, method: 'POST', }) const res = await app.request(req) expect(res).not.toBeNull() expect(res.status).toBe(200) expect(await res.text()).toBe('123 is valid!') }) it('Should return 400 response', async () => { const req = new Request('http://localhost/post', { body: JSON.stringify({ id: '123', title: 'Hello', }), headers: { 'Content-Type': 'application/json', }, method: 'POST', }) const res = await app.request(req) expect(res).not.toBeNull() expect(res.status).toBe(400) }) })