Skip to content

Commit

Permalink
Merge pull request #9 from sandros94/fix-build-issues
Browse files Browse the repository at this point in the history
Fix build issues, better vitests and new UUID util
  • Loading branch information
Intevel authored Dec 13, 2024
2 parents be3b186 + 3defbdc commit 88f18f1
Show file tree
Hide file tree
Showing 12 changed files with 145 additions and 1,368 deletions.
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
"validation"
],
"sideEffects": false,
"build": {
"externals": [
"valibot"
]
},
"exports": {
".": {
"types": "./dist/index.d.ts",
Expand All @@ -34,11 +39,10 @@
},
"dependencies": {
"h3": "^1.13.0",
"valibot": "^1.0.0-beta.3"
"valibot": "^1.0.0-beta.9"
},
"devDependencies": {
"@antfu/eslint-config": "3.8.0",
"@nuxtjs/eslint-config-typescript": "latest",
"@types/node": "^20",
"@types/serve-handler": "6.1.4",
"@typescript-eslint/eslint-plugin": "8.14.0",
Expand Down
1,208 changes: 6 additions & 1,202 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import {
checkboxAsString,
intAsString,
numAsString,
uuid,
} from './schemas'

export const vh = {
boolAsString,
checkboxAsString,
intAsString,
numAsString,
uuid,
}

export {
Expand Down
13 changes: 12 additions & 1 deletion src/schemas.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Original code by https://github.com/rileytomasek/zodix/blob/31bd5f2708f2eec4d0522201710dfc5ef887a16e/src/schemas.ts
import * as v from 'Valibot'
import * as v from 'valibot'

/**
* Valibot schema to parse strings that are booleans.
Expand Down Expand Up @@ -56,3 +56,14 @@ export const numAsString = v.pipe(
v.decimal('Must be a number string'),
v.transform(Number),
)

/**
* Valibot schema to parse strings that are valid UUID.
* @example
* ```ts
* v.parse(vh.uuid, '2') -> throws an error
*/
export const uuid = v.pipe(
v.string(),
v.uuid(),
)
60 changes: 0 additions & 60 deletions test/__snapshots__/body.test.ts.snap

This file was deleted.

25 changes: 0 additions & 25 deletions test/__snapshots__/params.test.ts.snap

This file was deleted.

60 changes: 0 additions & 60 deletions test/__snapshots__/query.test.ts.snap

This file was deleted.

32 changes: 29 additions & 3 deletions test/body.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('useValidatedBody', () => {
const res = await request.post('/validate').send({ required: true })

expect(res.status).toEqual(200)
expect(res.body).toMatchSnapshot()
expect(res.body).toEqual({ required: true })
})

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

expect(res.status).toEqual(400)
expect(res.body).toMatchSnapshot()
expect(res.body).toMatchInlineSnapshot(`
{
"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",
}
`)
})

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

expect(res.status).toEqual(200)
expect(res.body).toMatchSnapshot()
expect(res.body).toEqual(v.safeParse(bodySchema, {}))
})
})
48 changes: 42 additions & 6 deletions test/params.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { App, Router } from 'h3'
import { createApp, createRouter, defineEventHandler, toNodeListener } from 'h3'
import supertest from 'supertest'
import { beforeEach, describe, expect, it } from 'vitest'
import { useSafeValidatedParams, useValidatedParams, v } from '../src'
import { useSafeValidatedParams, useValidatedParams, v, vh } from '../src'

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

expect(res.status).toEqual(200)
expect(res.body).toMatchSnapshot()
expect(res.body).toEqual({ name: 'sandros94' })
})

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

expect(res.status).toEqual(404)
expect(res.body).toMatchSnapshot()
expect(res.body).toEqual({
stack: [],
statusCode: 404,
statusMessage: 'Cannot find any path matching /validate.',
})
})

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

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

expect(res.status).toEqual(200)
expect(res.body).toMatchSnapshot()
expect(res.body).toMatchInlineSnapshot(`
{
"issues": [
{
"expected": null,
"input": "2",
"kind": "validation",
"message": "Invalid UUID: Received "2"",
"path": [
{
"input": {
"id": "2",
},
"key": "id",
"origin": "value",
"type": "object",
"value": "2",
},
],
"received": ""2"",
"requirement": {},
"type": "uuid",
},
],
"output": {
"id": "2",
},
"success": false,
"typed": true,
}
`)
})
})
32 changes: 29 additions & 3 deletions test/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('useValidatedQuery', () => {
const res = await request.get('/validate?required')

expect(res.status).toEqual(200)
expect(res.body).toMatchSnapshot()
expect(res.body).toEqual({ required: '' })
})

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

expect(res.status).toEqual(400)
expect(res.body).toMatchSnapshot()
expect(res.body).toMatchInlineSnapshot(`
{
"data": {
"issues": [
{
"expected": "string",
"kind": "schema",
"message": "Invalid type: Expected string but received undefined",
"path": [
{
"input": {},
"key": "required",
"origin": "value",
"type": "object",
},
],
"received": "undefined",
"type": "string",
},
],
"name": "ValiError",
},
"stack": [],
"statusCode": 400,
"statusMessage": "Bad Request",
}
`)
})

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

expect(res.status).toEqual(200)
expect(res.body).toMatchSnapshot()
expect(res.body).toEqual(v.safeParse(querySchema, {}))
})
})
Loading

0 comments on commit 88f18f1

Please sign in to comment.