Skip to content

Commit 88f18f1

Browse files
authored
Merge pull request #9 from sandros94/fix-build-issues
Fix build issues, better vitests and new UUID util
2 parents be3b186 + 3defbdc commit 88f18f1

12 files changed

+145
-1368
lines changed

package.json

+6-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
"validation"
1212
],
1313
"sideEffects": false,
14+
"build": {
15+
"externals": [
16+
"valibot"
17+
]
18+
},
1419
"exports": {
1520
".": {
1621
"types": "./dist/index.d.ts",
@@ -34,11 +39,10 @@
3439
},
3540
"dependencies": {
3641
"h3": "^1.13.0",
37-
"valibot": "^1.0.0-beta.3"
42+
"valibot": "^1.0.0-beta.9"
3843
},
3944
"devDependencies": {
4045
"@antfu/eslint-config": "3.8.0",
41-
"@nuxtjs/eslint-config-typescript": "latest",
4246
"@types/node": "^20",
4347
"@types/serve-handler": "6.1.4",
4448
"@typescript-eslint/eslint-plugin": "8.14.0",

pnpm-lock.yaml

+6-1,202
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ import {
33
checkboxAsString,
44
intAsString,
55
numAsString,
6+
uuid,
67
} from './schemas'
78

89
export const vh = {
910
boolAsString,
1011
checkboxAsString,
1112
intAsString,
1213
numAsString,
14+
uuid,
1315
}
1416

1517
export {

src/schemas.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Original code by https://github.com/rileytomasek/zodix/blob/31bd5f2708f2eec4d0522201710dfc5ef887a16e/src/schemas.ts
2-
import * as v from 'Valibot'
2+
import * as v from 'valibot'
33

44
/**
55
* Valibot schema to parse strings that are booleans.
@@ -56,3 +56,14 @@ export const numAsString = v.pipe(
5656
v.decimal('Must be a number string'),
5757
v.transform(Number),
5858
)
59+
60+
/**
61+
* Valibot schema to parse strings that are valid UUID.
62+
* @example
63+
* ```ts
64+
* v.parse(vh.uuid, '2') -> throws an error
65+
*/
66+
export const uuid = v.pipe(
67+
v.string(),
68+
v.uuid(),
69+
)

test/__snapshots__/body.test.ts.snap

-60
This file was deleted.

test/__snapshots__/params.test.ts.snap

-25
This file was deleted.

test/__snapshots__/query.test.ts.snap

-60
This file was deleted.

test/body.test.ts

+29-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ describe('useValidatedBody', () => {
2424
const res = await request.post('/validate').send({ required: true })
2525

2626
expect(res.status).toEqual(200)
27-
expect(res.body).toMatchSnapshot()
27+
expect(res.body).toEqual({ required: true })
2828
})
2929

3030
it('throws 400 Bad Request if body does not match validation schema', async () => {
@@ -33,7 +33,33 @@ describe('useValidatedBody', () => {
3333
const res = await request.post('/validate').send({})
3434

3535
expect(res.status).toEqual(400)
36-
expect(res.body).toMatchSnapshot()
36+
expect(res.body).toMatchInlineSnapshot(`
37+
{
38+
"data": {
39+
"issues": [
40+
{
41+
"expected": "boolean",
42+
"kind": "schema",
43+
"message": "Invalid type: Expected boolean but received undefined",
44+
"path": [
45+
{
46+
"input": {},
47+
"key": "required",
48+
"origin": "value",
49+
"type": "object",
50+
},
51+
],
52+
"received": "undefined",
53+
"type": "boolean",
54+
},
55+
],
56+
"name": "ValiError",
57+
},
58+
"stack": [],
59+
"statusCode": 400,
60+
"statusMessage": "Bad Request",
61+
}
62+
`)
3763
})
3864

3965
it('doesn\'t throw 400 Bad Request if body does not match validation schema', async () => {
@@ -42,6 +68,6 @@ describe('useValidatedBody', () => {
4268
const res = await request.post('/validate').send({})
4369

4470
expect(res.status).toEqual(200)
45-
expect(res.body).toMatchSnapshot()
71+
expect(res.body).toEqual(v.safeParse(bodySchema, {}))
4672
})
4773
})

test/params.test.ts

+42-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { App, Router } from 'h3'
22
import { createApp, createRouter, defineEventHandler, toNodeListener } from 'h3'
33
import supertest from 'supertest'
44
import { beforeEach, describe, expect, it } from 'vitest'
5-
import { useSafeValidatedParams, useValidatedParams, v } from '../src'
5+
import { useSafeValidatedParams, useValidatedParams, v, vh } from '../src'
66

77
describe('useValidatedParams', () => {
88
let app: App
@@ -26,7 +26,7 @@ describe('useValidatedParams', () => {
2626
const res = await request.get('/validate/sandros94')
2727

2828
expect(res.status).toEqual(200)
29-
expect(res.body).toMatchSnapshot()
29+
expect(res.body).toEqual({ name: 'sandros94' })
3030
})
3131

3232
it('throws 400 Bad Request if params does not match validation schema', async () => {
@@ -35,15 +35,51 @@ describe('useValidatedParams', () => {
3535
const res = await request.get('/validate')
3636

3737
expect(res.status).toEqual(404)
38-
expect(res.body).toMatchSnapshot()
38+
expect(res.body).toEqual({
39+
stack: [],
40+
statusCode: 404,
41+
statusMessage: 'Cannot find any path matching /validate.',
42+
})
3943
})
4044

4145
it('doesn\'t throw 400 Bad Request if params does not match validation schema', async () => {
42-
router.get('/validate/:name', defineEventHandler(event => useSafeValidatedParams(event, paramsSchema)))
46+
router.get('/validate/:id', defineEventHandler(event => useSafeValidatedParams(event, v.object({
47+
id: vh.uuid,
48+
}))))
4349

44-
const res = await request.get('/validate/2') // TODO: this is not actually correct
50+
const res = await request.get('/validate/2')
4551

4652
expect(res.status).toEqual(200)
47-
expect(res.body).toMatchSnapshot()
53+
expect(res.body).toMatchInlineSnapshot(`
54+
{
55+
"issues": [
56+
{
57+
"expected": null,
58+
"input": "2",
59+
"kind": "validation",
60+
"message": "Invalid UUID: Received "2"",
61+
"path": [
62+
{
63+
"input": {
64+
"id": "2",
65+
},
66+
"key": "id",
67+
"origin": "value",
68+
"type": "object",
69+
"value": "2",
70+
},
71+
],
72+
"received": ""2"",
73+
"requirement": {},
74+
"type": "uuid",
75+
},
76+
],
77+
"output": {
78+
"id": "2",
79+
},
80+
"success": false,
81+
"typed": true,
82+
}
83+
`)
4884
})
4985
})

test/query.test.ts

+29-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe('useValidatedQuery', () => {
2323
const res = await request.get('/validate?required')
2424

2525
expect(res.status).toEqual(200)
26-
expect(res.body).toMatchSnapshot()
26+
expect(res.body).toEqual({ required: '' })
2727
})
2828

2929
it('throws 400 Bad Request if query does not match validation schema', async () => {
@@ -32,7 +32,33 @@ describe('useValidatedQuery', () => {
3232
const res = await request.get('/validate')
3333

3434
expect(res.status).toEqual(400)
35-
expect(res.body).toMatchSnapshot()
35+
expect(res.body).toMatchInlineSnapshot(`
36+
{
37+
"data": {
38+
"issues": [
39+
{
40+
"expected": "string",
41+
"kind": "schema",
42+
"message": "Invalid type: Expected string but received undefined",
43+
"path": [
44+
{
45+
"input": {},
46+
"key": "required",
47+
"origin": "value",
48+
"type": "object",
49+
},
50+
],
51+
"received": "undefined",
52+
"type": "string",
53+
},
54+
],
55+
"name": "ValiError",
56+
},
57+
"stack": [],
58+
"statusCode": 400,
59+
"statusMessage": "Bad Request",
60+
}
61+
`)
3662
})
3763

3864
it('doesn\'t throw 400 Bad Request if query does not match validation schema', async () => {
@@ -41,6 +67,6 @@ describe('useValidatedQuery', () => {
4167
const res = await request.get('/validate')
4268

4369
expect(res.status).toEqual(200)
44-
expect(res.body).toMatchSnapshot()
70+
expect(res.body).toEqual(v.safeParse(querySchema, {}))
4571
})
4672
})

0 commit comments

Comments
 (0)