|
| 1 | +import { Hono } from 'hono' |
| 2 | +import type { Equal, Expect } from 'hono/utils/types' |
| 3 | +import { z } from 'zod' |
| 4 | +import { schemaValidator } from '../src' |
| 5 | + |
| 6 | +// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 7 | +type ExtractSchema<T> = T extends Hono<infer _, infer S> ? S : never |
| 8 | + |
| 9 | +describe('Basic', () => { |
| 10 | + const app = new Hono() |
| 11 | + |
| 12 | + const jsonSchema = z.object({ |
| 13 | + name: z.string(), |
| 14 | + age: z.number(), |
| 15 | + }) |
| 16 | + |
| 17 | + const querySchema = z |
| 18 | + .object({ |
| 19 | + name: z.string().optional(), |
| 20 | + }) |
| 21 | + .optional() |
| 22 | + |
| 23 | + const route = app.post( |
| 24 | + '/author', |
| 25 | + schemaValidator('json', jsonSchema), |
| 26 | + schemaValidator('query', querySchema), |
| 27 | + (c) => { |
| 28 | + const data = c.req.valid('json') |
| 29 | + const query = c.req.valid('query') |
| 30 | + |
| 31 | + return c.json({ |
| 32 | + success: true, |
| 33 | + message: `${data.name} is ${data.age}`, |
| 34 | + queryName: query?.name, |
| 35 | + }) |
| 36 | + } |
| 37 | + ) |
| 38 | + |
| 39 | + type Actual = ExtractSchema<typeof route> |
| 40 | + type Expected = { |
| 41 | + '/author': { |
| 42 | + $post: { |
| 43 | + input: { |
| 44 | + json: { |
| 45 | + name: string |
| 46 | + age: number |
| 47 | + } |
| 48 | + } & { |
| 49 | + query?: |
| 50 | + | { |
| 51 | + name?: string | undefined |
| 52 | + } |
| 53 | + | undefined |
| 54 | + } |
| 55 | + output: { |
| 56 | + success: boolean |
| 57 | + message: string |
| 58 | + queryName: string | undefined |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 65 | + type verify = Expect<Equal<Expected, Actual>> |
| 66 | + |
| 67 | + it('Should return 200 response', async () => { |
| 68 | + const req = new Request('http://localhost/author?name=Metallo', { |
| 69 | + body: JSON.stringify({ |
| 70 | + name: 'Superman', |
| 71 | + age: 20, |
| 72 | + }), |
| 73 | + method: 'POST', |
| 74 | + headers: { |
| 75 | + 'Content-Type': 'application/json', |
| 76 | + }, |
| 77 | + }) |
| 78 | + const res = await app.request(req) |
| 79 | + expect(res).not.toBeNull() |
| 80 | + expect(res.status).toBe(200) |
| 81 | + expect(await res.json()).toEqual({ |
| 82 | + success: true, |
| 83 | + message: 'Superman is 20', |
| 84 | + queryName: 'Metallo', |
| 85 | + }) |
| 86 | + }) |
| 87 | + |
| 88 | + it('Should return 400 response', async () => { |
| 89 | + const req = new Request('http://localhost/author', { |
| 90 | + body: JSON.stringify({ |
| 91 | + name: 'Superman', |
| 92 | + age: '20', |
| 93 | + }), |
| 94 | + method: 'POST', |
| 95 | + }) |
| 96 | + const res = await app.request(req) |
| 97 | + expect(res).not.toBeNull() |
| 98 | + expect(res.status).toBe(400) |
| 99 | + const data:{success:boolean} = await res.json() |
| 100 | + expect(data['success']).toBe(false) |
| 101 | + }) |
| 102 | +}) |
| 103 | + |
| 104 | +describe('With Hook', () => { |
| 105 | + const app = new Hono() |
| 106 | + |
| 107 | + const schema = z.object({ |
| 108 | + id: z.number(), |
| 109 | + title: z.string(), |
| 110 | + }) |
| 111 | + |
| 112 | + app.post( |
| 113 | + '/post', |
| 114 | + schemaValidator('json', schema, (result, c) => { |
| 115 | + if (!result.success) { |
| 116 | + return c.text(`${(result.inputData as {id:string}).id} is invalid!`, 400) |
| 117 | + } |
| 118 | + const data = result.data |
| 119 | + return c.text(`${data.id} is valid!`) |
| 120 | + }), |
| 121 | + (c) => { |
| 122 | + const data = c.req.valid('json') |
| 123 | + return c.json({ |
| 124 | + success: true, |
| 125 | + message: `${data.id} is ${data.title}`, |
| 126 | + }) |
| 127 | + } |
| 128 | + ) |
| 129 | + |
| 130 | + it('Should return 200 response', async () => { |
| 131 | + const req = new Request('http://localhost/post', { |
| 132 | + body: JSON.stringify({ |
| 133 | + id: 123, |
| 134 | + title: 'Hello', |
| 135 | + }), |
| 136 | + method: 'POST', |
| 137 | + headers: { |
| 138 | + 'Content-Type': 'application/json', |
| 139 | + }, |
| 140 | + }) |
| 141 | + const res = await app.request(req) |
| 142 | + expect(res).not.toBeNull() |
| 143 | + expect(res.status).toBe(200) |
| 144 | + expect(await res.text()).toBe('123 is valid!') |
| 145 | + }) |
| 146 | + |
| 147 | + it('Should return 400 response', async () => { |
| 148 | + const req = new Request('http://localhost/post', { |
| 149 | + body: JSON.stringify({ |
| 150 | + id: '123', |
| 151 | + title: 'Hello', |
| 152 | + }), |
| 153 | + method: 'POST', |
| 154 | + headers: { |
| 155 | + 'Content-Type': 'application/json', |
| 156 | + }, |
| 157 | + }) |
| 158 | + const res = await app.request(req) |
| 159 | + expect(res).not.toBeNull() |
| 160 | + expect(res.status).toBe(400) |
| 161 | + expect(await res.text()).toBe('123 is invalid!') |
| 162 | + }) |
| 163 | +}) |
0 commit comments