forked from elierotenberg/fastify-zod
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update dependencies * Replace fastify-swagger with @fastify/swagger * Add support for querystring * Add separate tests for issues * Fix elierotenberg#14, elierotenberg#17
- Loading branch information
1 parent
2c0be0c
commit 4602f15
Showing
8 changed files
with
344 additions
and
224 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,133 @@ | ||
import fastify from "fastify"; | ||
import { z } from "zod"; | ||
|
||
import { buildJsonSchemas, register } from ".."; | ||
|
||
test(`fix #8`, () => { | ||
const productInput = { | ||
title: z.string(), | ||
price: z.number(), | ||
content: z.string().optional(), | ||
}; | ||
|
||
const productGenerated = { | ||
id: z.number(), | ||
createdAt: z.string(), | ||
updatedAt: z.string(), | ||
}; | ||
|
||
const createProductSchema = z.object({ | ||
...productInput, | ||
}); | ||
|
||
const productResponseSchema = z.object({ | ||
...productInput, | ||
...productGenerated, | ||
}); | ||
|
||
const productsResponseSchema = z.array(productResponseSchema); | ||
|
||
buildJsonSchemas({ | ||
createProductSchema, | ||
productResponseSchema, | ||
productsResponseSchema, | ||
}); | ||
|
||
const userCoreSchema = { | ||
email: z | ||
.string({ | ||
required_error: `Email is required`, | ||
invalid_type_error: `Email must be a string`, | ||
}) | ||
.email(), | ||
name: z.string(), | ||
}; | ||
|
||
const createUserSchema = z.object({ | ||
...userCoreSchema, | ||
password: z.string({ | ||
required_error: `Password is required`, | ||
invalid_type_error: `Password must be a string`, | ||
}), | ||
}); | ||
|
||
const createUserResponseSchema = z.object({ | ||
...userCoreSchema, | ||
id: z.number(), | ||
}); | ||
|
||
const loginSchema = z.object({ | ||
email: z | ||
.string({ | ||
required_error: `Email is required`, | ||
invalid_type_error: `Email must be a string`, | ||
}) | ||
.email(), | ||
password: z.string(), | ||
}); | ||
|
||
const loginResponseSchema = z.object({ | ||
accessToken: z.string(), | ||
}); | ||
|
||
buildJsonSchemas({ | ||
createUserSchema, | ||
createUserResponseSchema, | ||
loginSchema, | ||
loginResponseSchema, | ||
}); | ||
}); | ||
|
||
test(`fix #14, #17`, async () => { | ||
const Name = z.object({ | ||
kind: z.literal(`name`), | ||
name: z.string(), | ||
lastName: z.string(), | ||
}); | ||
|
||
const Address = z.object({ | ||
kind: z.literal(`address`), | ||
street: z.string(), | ||
postcode: z.string(), | ||
}); | ||
|
||
const UserDetails = z.union([Name, Address]); | ||
|
||
const Unknown = z.unknown(); | ||
|
||
const jsonSchemas = buildJsonSchemas({ UserDetails, Unknown }, {}); | ||
|
||
const f = register(fastify(), { | ||
jsonSchemas, | ||
}); | ||
|
||
f.zod.get( | ||
`/`, | ||
{ | ||
operationId: `getUserDetails`, | ||
querystring: `UserDetails`, | ||
reply: `UserDetails`, | ||
}, | ||
async ({ query }) => query, | ||
); | ||
|
||
const name = await f | ||
.inject({ method: `get`, url: `/`, query: { kind: `name` } }) | ||
.then((res) => res.json()); | ||
|
||
expect(name).toEqual({ | ||
error: `Bad Request`, | ||
message: `querystring should have required property 'name', querystring.kind should be equal to constant, querystring should match some schema in anyOf`, | ||
statusCode: 400, | ||
}); | ||
|
||
const address = await f | ||
.inject({ method: `get`, url: `/`, query: { kind: `address` } }) | ||
.then((res) => res.json()); | ||
|
||
expect(address).toEqual({ | ||
error: `Bad Request`, | ||
message: `querystring.kind should be equal to constant, querystring should have required property 'street', querystring should match some schema in anyOf`, | ||
statusCode: 400, | ||
}); | ||
}); |
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
Oops, something went wrong.