2023-07-30 07:07:44 +08:00
|
|
|
import { Hono } from 'hono'
|
2024-12-25 17:08:43 +08:00
|
|
|
import type { StatusCode } from 'hono/utils/http-status'
|
2023-07-30 07:07:44 +08:00
|
|
|
import type { Equal, Expect } from 'hono/utils/types'
|
2024-06-09 12:15:41 +08:00
|
|
|
import { number, object, objectAsync, optional, optionalAsync, string } from 'valibot'
|
2023-07-30 07:07:44 +08:00
|
|
|
import { vValidator } from '../src'
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
|
|
type ExtractSchema<T> = T extends Hono<infer _, infer S> ? S : never
|
|
|
|
|
|
|
|
describe('Basic', () => {
|
|
|
|
const app = new Hono()
|
|
|
|
|
|
|
|
const schema = object({
|
|
|
|
name: string(),
|
|
|
|
age: number(),
|
|
|
|
})
|
|
|
|
|
2024-02-15 04:38:43 +08:00
|
|
|
const querySchema = optional(
|
|
|
|
object({
|
|
|
|
search: optional(string()),
|
2024-05-06 09:10:56 +08:00
|
|
|
page: optional(number()),
|
2023-07-30 07:07:44 +08:00
|
|
|
})
|
2024-02-15 04:38:43 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const route = app.post(
|
|
|
|
'/author',
|
|
|
|
vValidator('json', schema),
|
|
|
|
vValidator('query', querySchema),
|
|
|
|
(c) => {
|
|
|
|
const data = c.req.valid('json')
|
|
|
|
const query = c.req.valid('query')
|
|
|
|
|
|
|
|
return c.json({
|
|
|
|
success: true,
|
|
|
|
message: `${data.name} is ${data.age}, search is ${query?.search}`,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
2023-07-30 07:07:44 +08:00
|
|
|
|
|
|
|
type Actual = ExtractSchema<typeof route>
|
|
|
|
type Expected = {
|
|
|
|
'/author': {
|
|
|
|
$post: {
|
|
|
|
input: {
|
|
|
|
json: {
|
|
|
|
name: string
|
|
|
|
age: number
|
|
|
|
}
|
2024-02-15 04:38:43 +08:00
|
|
|
} & {
|
|
|
|
query?:
|
|
|
|
| {
|
2024-05-06 09:10:56 +08:00
|
|
|
search?: string | string[] | undefined
|
|
|
|
page?: string | string[] | undefined
|
2024-02-15 04:38:43 +08:00
|
|
|
}
|
|
|
|
| undefined
|
2023-07-30 07:07:44 +08:00
|
|
|
}
|
|
|
|
output: {
|
2023-12-13 14:54:00 +08:00
|
|
|
success: boolean
|
2023-07-30 07:07:44 +08:00
|
|
|
message: string
|
|
|
|
}
|
2024-08-21 19:20:37 +08:00
|
|
|
status: StatusCode
|
|
|
|
outputFormat: 'json'
|
2023-07-30 07:07:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
|
|
type verify = Expect<Equal<Expected, Actual>>
|
|
|
|
|
|
|
|
it('Should return 200 response', async () => {
|
2024-02-15 04:38:43 +08:00
|
|
|
const req = new Request('http://localhost/author?search=hello', {
|
2023-07-30 07:07:44 +08:00
|
|
|
body: JSON.stringify({
|
|
|
|
name: 'Superman',
|
|
|
|
age: 20,
|
|
|
|
}),
|
2023-12-13 14:54:00 +08:00
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
2023-07-30 07:07:44 +08:00
|
|
|
method: 'POST',
|
|
|
|
})
|
|
|
|
const res = await app.request(req)
|
|
|
|
expect(res).not.toBeNull()
|
|
|
|
expect(res.status).toBe(200)
|
|
|
|
expect(await res.json()).toEqual({
|
|
|
|
success: true,
|
2024-02-15 04:38:43 +08:00
|
|
|
message: 'Superman is 20, search is hello',
|
2023-07-30 07:07:44 +08:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should return 400 response', async () => {
|
|
|
|
const req = new Request('http://localhost/author', {
|
|
|
|
body: JSON.stringify({
|
|
|
|
name: 'Superman',
|
|
|
|
age: '20',
|
|
|
|
}),
|
2023-12-13 14:54:00 +08:00
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
2023-07-30 07:07:44 +08:00
|
|
|
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)
|
|
|
|
}
|
2023-08-24 07:38:25 +08:00
|
|
|
const data = result.output
|
2023-07-30 07:07:44 +08:00
|
|
|
return c.text(`${data.id} is valid!`)
|
2024-05-13 05:56:42 +08:00
|
|
|
}),
|
|
|
|
(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)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('Async', () => {
|
|
|
|
const app = new Hono()
|
|
|
|
|
|
|
|
const schemaAsync = objectAsync({
|
2024-06-09 12:15:41 +08:00
|
|
|
name: string(),
|
|
|
|
age: number(),
|
2024-05-13 05:56:42 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
const querySchemaAsync = optionalAsync(
|
|
|
|
objectAsync({
|
2024-06-09 12:15:41 +08:00
|
|
|
search: optionalAsync(string()),
|
|
|
|
page: optionalAsync(number()),
|
2024-05-13 05:56:42 +08:00
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
const route = app.post(
|
|
|
|
'/author',
|
|
|
|
vValidator('json', schemaAsync),
|
|
|
|
vValidator('query', querySchemaAsync),
|
|
|
|
(c) => {
|
|
|
|
const data = c.req.valid('json')
|
|
|
|
const query = c.req.valid('query')
|
|
|
|
|
|
|
|
return c.json({
|
|
|
|
success: true,
|
|
|
|
message: `${data.name} is ${data.age}, search is ${query?.search}`,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
type Actual = ExtractSchema<typeof route>
|
|
|
|
type Expected = {
|
|
|
|
'/author': {
|
|
|
|
$post: {
|
|
|
|
input: {
|
|
|
|
json: {
|
|
|
|
name: string
|
|
|
|
age: number
|
|
|
|
}
|
|
|
|
} & {
|
|
|
|
query?:
|
|
|
|
| {
|
|
|
|
search?: string | string[] | undefined
|
|
|
|
page?: string | string[] | undefined
|
|
|
|
}
|
|
|
|
| undefined
|
|
|
|
}
|
|
|
|
output: {
|
|
|
|
success: boolean
|
|
|
|
message: string
|
|
|
|
}
|
2024-08-21 19:20:37 +08:00
|
|
|
status: StatusCode
|
|
|
|
outputFormat: 'json'
|
2024-05-13 05:56:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
|
|
type verify = Expect<Equal<Expected, Actual>>
|
|
|
|
|
|
|
|
it('Should return 200 response', async () => {
|
|
|
|
const req = new Request('http://localhost/author?search=hello', {
|
|
|
|
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, search is hello',
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
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 Async', () => {
|
|
|
|
const app = new Hono()
|
|
|
|
|
|
|
|
const schemaAsync = objectAsync({
|
2024-06-09 12:15:41 +08:00
|
|
|
id: number(),
|
|
|
|
title: string(),
|
2024-05-13 05:56:42 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
app.post(
|
|
|
|
'/post',
|
|
|
|
vValidator('json', schemaAsync, (result, c) => {
|
|
|
|
if (!result.success) {
|
|
|
|
return c.text('Invalid!', 400)
|
|
|
|
}
|
|
|
|
const data = result.output
|
|
|
|
return c.text(`${data.id} is valid!`)
|
2023-07-30 07:07:44 +08:00
|
|
|
}),
|
|
|
|
(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',
|
|
|
|
}),
|
2023-12-13 14:54:00 +08:00
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
2023-07-30 07:07:44 +08:00
|
|
|
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',
|
|
|
|
}),
|
2023-12-13 14:54:00 +08:00
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
2023-07-30 07:07:44 +08:00
|
|
|
method: 'POST',
|
|
|
|
})
|
|
|
|
const res = await app.request(req)
|
|
|
|
expect(res).not.toBeNull()
|
|
|
|
expect(res.status).toBe(400)
|
|
|
|
})
|
|
|
|
})
|
2025-01-08 18:48:44 +08:00
|
|
|
|
|
|
|
describe('Test types', () => {
|
|
|
|
it('Should return correct types when validating a query', () => {
|
|
|
|
const app = new Hono()
|
|
|
|
|
|
|
|
const routes = app.post(
|
|
|
|
'/',
|
|
|
|
vValidator(
|
|
|
|
'query',
|
|
|
|
object({
|
|
|
|
foo: string(),
|
|
|
|
})
|
|
|
|
),
|
|
|
|
(c) => {
|
|
|
|
return c.json(c.req.valid('query'))
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
type T = ExtractSchema<typeof routes>
|
|
|
|
|
|
|
|
type Actual = T['/']['$post']['input']['query']
|
|
|
|
type Expected = { foo: string }
|
|
|
|
type verify = Expect<Equal<Expected, Actual>>
|
|
|
|
})
|
|
|
|
})
|