Skip to content

Commit

Permalink
feat: add schemas with zod (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreBerger committed Jan 31, 2024
1 parent 08bca10 commit e72886c
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 7 deletions.
Binary file modified bun.lockb
Binary file not shown.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
"fastify": "^4.0.0",
"fastify-cli": "^6.0.1",
"fastify-plugin": "^4.0.0",
"pino": "^8.17.2"
"fastify-type-provider-zod": "^1.1.9",
"pino": "^8.17.2",
"zod": "^3.22.4"
},
"devDependencies": {
"@types/bun": "latest",
Expand Down
7 changes: 7 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import * as path from 'path'
import AutoLoad from '@fastify/autoload'
import { type FastifyPluginAsync } from 'fastify'
import {
serializerCompiler,
validatorCompiler,
} from 'fastify-type-provider-zod'

const app: FastifyPluginAsync = async (fastify, opts): Promise<void> => {
fastify.register(AutoLoad, {
dir: path.join(__dirname, 'plugins'),
options: opts,
})

fastify.setValidatorCompiler(validatorCompiler)
fastify.setSerializerCompiler(serializerCompiler)

fastify.register(AutoLoad, {
dir: path.join(__dirname, 'routes'),
options: opts,
Expand Down
10 changes: 10 additions & 0 deletions src/domains/users/db/createUser.db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { PrismaClient } from '@prisma/client'
import type { CreateUserSchema } from '../schemas/user.schema'

export const createUser = (prisma: PrismaClient, user: CreateUserSchema) =>
prisma.user.create({
data: {
email: user.email,
name: user.name,
},
})
3 changes: 3 additions & 0 deletions src/domains/users/db/findAllUsers.db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import type { PrismaClient } from '@prisma/client'

export const findAllUsers = (prisma: PrismaClient) => prisma.user.findMany()
25 changes: 25 additions & 0 deletions src/domains/users/schemas/user.schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { z } from 'zod'

const user = {
email: z
.string({
required_error: 'Email is required',
invalid_type_error: 'Email must be a string',
})
.email(),
name: z
.string({
invalid_type_error: 'Name must be a string',
})
.nullish(),
}

export const findAllUsersSchema = z.array(z.object({ ...user, id: z.string() }))

export const createUserSchema = z.object(user)
export const createUserResponseSchema = z.object({
id: z.string(),
...user,
})

export type CreateUserSchema = z.infer<typeof createUserSchema>
14 changes: 11 additions & 3 deletions src/routes/example/index.route.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import type { FastifyPluginAsync } from 'fastify'
import type { ZodTypeProvider } from 'fastify-type-provider-zod'
import { z } from 'zod'

const example: FastifyPluginAsync = async (fastify, _opts): Promise<void> => {
fastify.get('/', async (_request, _reply) => {
return 'this is an example'
})
fastify
.withTypeProvider<ZodTypeProvider>()
.get(
'/',
{ schema: { response: { 200: z.string() } } },
async (_request, _reply) => {
return 'this is an example'
},
)
}

export default example
32 changes: 29 additions & 3 deletions src/routes/users/users.route.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,35 @@
import type { FastifyPluginAsync } from 'fastify'
import type { ZodTypeProvider } from 'fastify-type-provider-zod'
import { findAllUsers } from '../../domains/users/db/findAllUsers.db'
import {
createUserResponseSchema,
createUserSchema,
findAllUsersSchema,
} from '../../domains/users/schemas/user.schema'
import { createUser } from '../../domains/users/db/createUser.db'

const users: FastifyPluginAsync = async (fastify, _opts) => {
fastify.get('/', async (_request, _reply) => {
return fastify.prisma.user.findMany()
})
fastify
.withTypeProvider<ZodTypeProvider>()
.get('/', { schema: { response: { 200: findAllUsersSchema } } }, () => {
return findAllUsers(fastify.prisma)
})

fastify.withTypeProvider<ZodTypeProvider>().post(
'/',
{
schema: {
body: createUserSchema,
response: {
201: createUserResponseSchema,
},
},
},
async (request, reply) => {
const user = await createUser(fastify.prisma, request.body)
reply.code(201).send(user)
},
)
}

export default users

0 comments on commit e72886c

Please sign in to comment.