Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
node-version: [14, 16, 18, 20]
node-version: [14, 16, 18, 20, 21]
os: [macos-latest, ubuntu-latest, windows-latest]

steps:
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/linting.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
node-version: [18]
node-version: [20]
os: [ubuntu-latest]

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Use Node.js
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}

Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
"peerDependencies": {
"fastify": "^4.0.0",
"zod": "^3.14.2"
"zod": "^3.22.4"
},
"repository": {
"url": "https://github.com/turkerdev/fastify-type-provider-zod"
Expand All @@ -34,7 +34,7 @@
},
"homepage": "https://github.com/turkerdev/fastify-type-provider-zod",
"dependencies": {
"zod-to-json-schema": "^3.17.1"
"zod-to-json-schema": "3.22.0"
},
"devDependencies": {
"@fastify/swagger": "^8.12.0",
Expand All @@ -54,7 +54,7 @@
"ts-jest": "^29.1.1",
"tsd": "^0.29.0",
"typescript": "^5.3.2",
"zod": "^3.22.4"
"zod": "3.22.4"
},
"tsd": {
"directory": "types"
Expand Down
2 changes: 1 addition & 1 deletion test/__snapshots__/request-schema.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`response schema returns 400 on validation error 1`] = `
exports[`request schema returns 400 on validation error 1`] = `
{
"code": "FST_ERR_VALIDATION",
"error": "Bad Request",
Expand Down
91 changes: 83 additions & 8 deletions test/request-schema.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,35 @@ import { z } from 'zod';
import type { ZodTypeProvider } from '../src';
import { serializerCompiler, validatorCompiler } from '../src';

describe('response schema', () => {
const REQUEST_SCHEMA = z.object({
name: z.string(),
readonlyId: z.preprocess((value) => {
if (Array.isArray(value)) {
return value;
}
return [value];
}, z.array(z.string().readonly())),
});

const schema = z.object({
foo: z.boolean(),
bar: z.number(),
});

const REQUEST_BODY_SCHEMA = z.object({
name: z.string(),
readonlyObject: z
.object({
id: z.string(),
})
.readonly(),
valueRoArray: schema.array().readonly(),
valueRo: schema.readonly(),
});

describe('request schema', () => {
let app: FastifyInstance;
beforeAll(async () => {
const REQUEST_SCHEMA = z.object({
name: z.string(),
});

app = Fastify();
app.setValidatorCompiler(validatorCompiler);
app.setSerializerCompiler(serializerCompiler);
Expand All @@ -28,6 +50,22 @@ describe('response schema', () => {
handler: (req, res) => {
res.send({
name: req.query.name,
readonlyId: req.query.readonlyId[0],
});
},
})
.route({
method: 'POST',
url: '/',
schema: {
body: REQUEST_BODY_SCHEMA,
},
handler: (req, res) => {
res.send({
name: req.body.name,
readonlyId: req.body.readonlyObject.id,
arrId: req.body.valueRoArray[0].bar,
objId: req.body.valueRo.bar,
});
},
})
Expand All @@ -49,14 +87,49 @@ describe('response schema', () => {
await app.close();
});

it('accepts correct request', async () => {
const response = await app.inject().get('/').query({
it('accepts correct request with query params', async () => {
const response = await app
.inject()
.get('/')
.query({
name: 'test',
readonlyId: ['ro'],
});

expect(response.statusCode).toBe(200);
expect(response.json()).toEqual({
name: 'test',
readonlyId: 'ro',
});
});

it('accepts correct body', async () => {
const response = await app
.inject()
.post('/')
.body({
name: 'test',
readonlyObject: {
id: 'ro',
},
valueRoArray: [
{
foo: false,
bar: 0,
},
],
valueRo: {
foo: true,
bar: 1,
},
});

expect(response.statusCode).toBe(200);
expect(response.json()).toEqual({
name: 'test',
readonlyId: 'ro',
arrId: 0,
objId: 1,
});
});

Expand All @@ -70,7 +143,9 @@ describe('response schema', () => {
});

it('returns 400 on validation error', async () => {
const response = await app.inject().get('/');
const response = await app.inject().get('/').query({
readonlyId: 'ro',
});

expect(response.statusCode).toBe(400);
expect(response.json()).toMatchSnapshot();
Expand Down