-
Notifications
You must be signed in to change notification settings - Fork 3
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
Showing
2 changed files
with
109 additions
and
0 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,60 @@ | ||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[`useValidatedBody > doesn't throw 400 Bad Request if body does not match validation schema 1`] = ` | ||
{ | ||
"issues": [ | ||
{ | ||
"expected": "boolean", | ||
"kind": "schema", | ||
"message": "Invalid type: Expected boolean but received undefined", | ||
"path": [ | ||
{ | ||
"input": {}, | ||
"key": "required", | ||
"origin": "value", | ||
"type": "object", | ||
}, | ||
], | ||
"received": "undefined", | ||
"type": "boolean", | ||
}, | ||
], | ||
"output": {}, | ||
"success": false, | ||
"typed": false, | ||
} | ||
`; | ||
|
||
exports[`useValidatedBody > returns 200 OK if body matches validation schema 1`] = ` | ||
{ | ||
"required": true, | ||
} | ||
`; | ||
|
||
exports[`useValidatedBody > throws 400 Bad Request if body does not match validation schema 1`] = ` | ||
{ | ||
"data": { | ||
"issues": [ | ||
{ | ||
"expected": "boolean", | ||
"kind": "schema", | ||
"message": "Invalid type: Expected boolean but received undefined", | ||
"path": [ | ||
{ | ||
"input": {}, | ||
"key": "required", | ||
"origin": "value", | ||
"type": "object", | ||
}, | ||
], | ||
"received": "undefined", | ||
"type": "boolean", | ||
}, | ||
], | ||
"name": "ValiError", | ||
}, | ||
"stack": [], | ||
"statusCode": 400, | ||
"statusMessage": "Bad Request", | ||
} | ||
`; |
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,49 @@ | ||
import type { App } from 'h3' | ||
import type { Test } from 'supertest' | ||
import type TestAgent from 'supertest/lib/agent' | ||
import { createApp, eventHandler, toNodeListener } from 'h3' | ||
import supertest from 'supertest' | ||
import { beforeEach, describe, expect, it } from 'vitest' | ||
import { useSafeValidatedBody, useValidatedBody, v } from '../src' | ||
|
||
describe('useValidatedBody', () => { | ||
let app: App | ||
let request: TestAgent<Test> | ||
|
||
beforeEach(() => { | ||
app = createApp({ debug: false }) | ||
request = supertest(toNodeListener(app)) | ||
}) | ||
|
||
const bodySchema = v.object({ | ||
optional: v.optional(v.string()), | ||
required: v.boolean(), | ||
}) | ||
|
||
it('returns 200 OK if body matches validation schema', async () => { | ||
app.use('/validate', eventHandler(event => useValidatedBody(event, bodySchema))) | ||
|
||
const res = await request.post('/validate').send({ required: true }) | ||
|
||
expect(res.status).toEqual(200) | ||
expect(res.body).toMatchSnapshot() | ||
}) | ||
|
||
it('throws 400 Bad Request if body does not match validation schema', async () => { | ||
app.use('/validate', eventHandler(event => useValidatedBody(event, bodySchema))) | ||
|
||
const res = await request.post('/validate').send({}) | ||
|
||
expect(res.status).toEqual(400) | ||
expect(res.body).toMatchSnapshot() | ||
}) | ||
|
||
it('doesn\'t throw 400 Bad Request if body does not match validation schema', async () => { | ||
app.use('/validate', eventHandler(event => useSafeValidatedBody(event, bodySchema))) | ||
|
||
const res = await request.post('/validate').send({}) | ||
|
||
expect(res.status).toEqual(200) | ||
expect(res.body).toMatchSnapshot() | ||
}) | ||
}) |