This repository has been archived by the owner on May 2, 2024. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e6ed0b5
commit c6a07a0
Showing
7 changed files
with
86 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
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) { | ||
const body = await useBody(event) | ||
const validate = useValidator().compile(schema) | ||
|
||
if (!validate(body)) { | ||
throw createError({ statusCode: 400, statusMessage: `body ${validate.errors[0].message}` }) | ||
} | ||
|
||
return body as Static<T> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,4 @@ | ||
import type { Static, TSchema } from '@sinclair/typebox' | ||
import { createError, useBody, CompatibilityEvent } from 'h3' | ||
import Ajv from 'ajv' | ||
|
||
const ajv = new Ajv({ | ||
keywords: ['kind', 'modifier'] | ||
}) | ||
|
||
export async function useValidatedBody<T extends TSchema> (event: CompatibilityEvent, schema: T) { | ||
const body = await useBody(event) | ||
const validate = ajv.compile(schema) | ||
|
||
if (!validate(body)) { | ||
throw createError({ statusCode: 400, statusMessage: `body ${validate.errors[0].message}` }) | ||
} | ||
|
||
return body as Static<T> | ||
} | ||
|
||
export { Type } from '@sinclair/typebox' | ||
|
||
export * from './body' | ||
export * from './query' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
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) { | ||
const query = useQuery(event) | ||
const validate = useValidator().compile(schema) | ||
|
||
if (!validate(query)) { | ||
throw createError({ statusCode: 400, statusMessage: `query ${validate.errors[0].message}` }) | ||
} | ||
|
||
return query as Static<T> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './validator' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import Ajv from 'ajv' | ||
|
||
let instance: Ajv | ||
|
||
export function useValidator () { | ||
if (!instance) { | ||
instance = new Ajv({ | ||
keywords: ['kind', 'modifier'] | ||
}) | ||
} | ||
|
||
return instance | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import supertest, { SuperTest, Test } from 'supertest' | ||
import { describe, beforeEach, it, expect } from 'vitest' | ||
import { createApp, App } from 'h3' | ||
import { useValidatedQuery, Type } from '../src' | ||
|
||
describe('useValidatedQuery', () => { | ||
let app: App | ||
let request: SuperTest<Test> | ||
|
||
beforeEach(() => { | ||
app = createApp({ debug: false }) | ||
request = supertest(app) | ||
}) | ||
|
||
const querySchema = Type.Object({ | ||
required: Type.String() | ||
}) | ||
|
||
it('returns 200 OK if query matches validation schema', async () => { | ||
app.use('/validate', req => useValidatedQuery(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)) | ||
|
||
const res = await request.get('/validate') | ||
|
||
expect(res.status).toEqual(400) | ||
expect(res.body).toEqual( | ||
expect.objectContaining({ | ||
statusMessage: "query must have required property 'required'" | ||
}) | ||
) | ||
}) | ||
}) |