Skip to content
This repository has been archived by the owner on May 2, 2024. It is now read-only.

Commit

Permalink
refactor: rename utilities and deprecate old ones
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinmarrec committed Aug 5, 2022
1 parent 771e4e5 commit bb1ad8b
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 13 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@ pnpm install h3-typebox
```js
import { createServer } from 'http'
import { createApp } from 'h3'
import { useValidatedBody, useValidatedQuery, Type } from 'h3-typebox'
import { validateBody, validateQuery, Type } from 'h3-typebox'

const app = createApp()
app.use('/', async (req) => {
// Validate body
const body = await useValidatedBody(req, Type.Object({
const body = await validateBody(req, Type.Object({
optional: Type.Optional(Type.String()),
required: Type.Boolean(),
}))

// Validate query
const query = useValidatedQuery(req, Type.Object({
const query = validateQuery(req, Type.Object({
required: Type.String(),
}))
})
Expand Down
6 changes: 5 additions & 1 deletion src/body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Static, TSchema } from '@sinclair/typebox'
import { createError, useBody, CompatibilityEvent } from 'h3'
import { useValidator } from './utils'

export async function useValidatedBody<T extends TSchema> (event: CompatibilityEvent, schema: T) {
export async function validateBody<T extends TSchema> (event: CompatibilityEvent, schema: T) {
const body = await useBody(event)
const validate = useValidator().compile(schema)

Expand All @@ -12,3 +12,7 @@ export async function useValidatedBody<T extends TSchema> (event: CompatibilityE

return body as Static<T>
}

/** @deprecated Use `validateBody` */
/* c8 ignore next */
export const useValidatedBody = validateBody
6 changes: 5 additions & 1 deletion src/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Static, TSchema } from '@sinclair/typebox'
import { createError, CompatibilityEvent, useQuery } from 'h3'
import { useValidator } from './utils'

export function useValidatedQuery<T extends TSchema> (event: CompatibilityEvent, schema: T) {
export function validateQuery<T extends TSchema> (event: CompatibilityEvent, schema: T) {
const query = useQuery(event)
const validate = useValidator().compile(schema)

Expand All @@ -12,3 +12,7 @@ export function useValidatedQuery<T extends TSchema> (event: CompatibilityEvent,

return query as Static<T>
}

/** @deprecated Use `validateQuery` */
/* c8 ignore next */
export const useValidatedQuery = validateQuery
8 changes: 4 additions & 4 deletions test/body.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import supertest, { SuperTest, Test } from 'supertest'
import { describe, beforeEach, it, expect } from 'vitest'
import { createApp, App } from 'h3'
import { useValidatedBody, Type } from '../src'
import { validateBody, Type } from '../src'

describe('useValidatedBody', () => {
describe('validateBody', () => {
let app: App
let request: SuperTest<Test>

Expand All @@ -18,15 +18,15 @@ describe('useValidatedBody', () => {
})

it('returns 200 OK if body matches validation schema', async () => {
app.use('/validate', async req => await useValidatedBody(req, bodySchema))
app.use('/validate', async req => await validateBody(req, bodySchema))

const res = await request.post('/validate').send({ required: true })

expect(res.status).toEqual(200)
})

it('throws 400 Bad Request if body does not match validation schema', async () => {
app.use('/validate', async req => await useValidatedBody(req, bodySchema))
app.use('/validate', async req => await validateBody(req, bodySchema))

const res = await request.post('/validate').send({})

Expand Down
8 changes: 4 additions & 4 deletions test/query.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import supertest, { SuperTest, Test } from 'supertest'
import { describe, beforeEach, it, expect } from 'vitest'
import { createApp, App } from 'h3'
import { useValidatedQuery, Type } from '../src'
import { validateQuery, Type } from '../src'

describe('useValidatedQuery', () => {
describe('validateQuery', () => {
let app: App
let request: SuperTest<Test>

Expand All @@ -17,15 +17,15 @@ describe('useValidatedQuery', () => {
})

it('returns 200 OK if query matches validation schema', async () => {
app.use('/validate', req => useValidatedQuery(req, querySchema))
app.use('/validate', req => validateQuery(req, querySchema))

const res = await request.get('/validate?required')

expect(res.status).toEqual(200)
})

it('throws 400 Bad Request if query does not match validation schema', async () => {
app.use('/validate', req => useValidatedQuery(req, querySchema))
app.use('/validate', req => validateQuery(req, querySchema))

const res = await request.get('/validate')

Expand Down

0 comments on commit bb1ad8b

Please sign in to comment.