Skip to content

Commit

Permalink
refactor(validation): move zod schemas to validation package
Browse files Browse the repository at this point in the history
  • Loading branch information
Quốc Khánh committed Jun 18, 2024
1 parent 2bed73f commit cbf63ec
Show file tree
Hide file tree
Showing 21 changed files with 406 additions and 498 deletions.
1 change: 0 additions & 1 deletion apps/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const app = new Hono({ strict: true })
// * Mounting versioned APIs
.route('/v1', appV1)

export * from './v1/validation'
export * from './prisma/generated/zod'
export { app }
export type AppType = typeof app
5 changes: 2 additions & 3 deletions apps/api/next.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
rewrites: () => [
{ source: '/v1/:path*', destination: '/api/v1/:path*' },
]
rewrites: () => [{ source: '/v1/:path*', destination: '/api/v1/:path*' }],
transpilePackages: ['@6pm/validation'],
}

module.exports = nextConfig
1 change: 1 addition & 0 deletions apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"postinstall": "prisma generate"
},
"dependencies": {
"@6pm/validation": "workspace:^",
"@clerk/backend": "^1.2.2",
"@hono/clerk-auth": "^2.0.0",
"@hono/node-server": "^1.11.2",
Expand Down
2 changes: 1 addition & 1 deletion apps/api/v1/routes/budgets.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { zCreateBudget, zCreateUser, zUpdateBudget } from '@6pm/validation'
import { zValidator } from '@hono/zod-validator'
import { BudgetUserPermission } from '@prisma/client'
import { Hono } from 'hono'
Expand Down Expand Up @@ -25,7 +26,6 @@ import {
findBudgetsOfUser,
updateBudget,
} from '../services/budget.service'
import { zCreateBudget, zCreateUser, zUpdateBudget } from '../validation'

const zBudgetParamValidator = zValidator(
'param',
Expand Down
8 changes: 4 additions & 4 deletions apps/api/v1/routes/transactions.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import {
zCreateTransaction,
zUpdateTransaction,
} from '@6pm/validation'
import { zValidator } from '@hono/zod-validator'
import { Hono } from 'hono'
import { z } from 'zod'
Expand All @@ -15,10 +19,6 @@ import {
updateTransaction,
} from '../services/transaction.service'
import { findUserWallet } from '../services/wallet.service'
import {
zCreateTransaction,
zUpdateTransaction,
} from '../validation/transaction.zod'

const router = new Hono()

Expand Down
2 changes: 1 addition & 1 deletion apps/api/v1/routes/users.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { zCreateUser } from '@6pm/validation'
import { zValidator } from '@hono/zod-validator'
import { Hono } from 'hono'
import { getAuthUser } from '../middlewares/auth'
import { createUser } from '../services/user.service'
import { zCreateUser } from '../validation'

const router = new Hono().post(
'/',
Expand Down
2 changes: 1 addition & 1 deletion apps/api/v1/routes/wallets.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { zCreateWallet, zUpdateWallet } from '@6pm/validation'
import { zValidator } from '@hono/zod-validator'
import { Hono } from 'hono'
import { object, string } from 'zod'
Expand All @@ -14,7 +15,6 @@ import {
updateWallet,
walletWithBalance,
} from '../services/wallet.service'
import { zCreateWallet, zUpdateWallet } from '../validation'

const router = new Hono()

Expand Down
2 changes: 1 addition & 1 deletion apps/api/v1/services/budget-invitation.service.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { CreateUser } from '@6pm/validation'
import {
type Budget,
type BudgetUserInvitation,
BudgetUserPermission,
type User,
} from '@prisma/client'
import prisma from '../../lib/prisma'
import type { CreateUser } from '../validation'
import { createBudgetUser, isUserBudgetOwner } from './budget.service'
import { createUser, findUserByEmail } from './user.service'

Expand Down
2 changes: 1 addition & 1 deletion apps/api/v1/services/budget.service.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { CreateBudget, UpdateBudget } from '@6pm/validation'
import {
type Budget,
BudgetPeriodType,
Expand All @@ -6,7 +7,6 @@ import {
} from '@prisma/client'
import { dayjsExtended } from '../../lib/dayjs'
import prisma from '../../lib/prisma'
import type { CreateBudget, UpdateBudget } from '../validation'
import { inviteUserToBudget } from './budget-invitation.service'

export async function canUserCreateBudget({
Expand Down
5 changes: 1 addition & 4 deletions apps/api/v1/services/transaction.service.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import type { CreateTransaction, UpdateTransaction } from '@6pm/validation'
import type {
Budget,
Transaction,
User,
UserWalletAccount,
} from '@prisma/client'
import prisma from '../../lib/prisma'
import type {
CreateTransaction,
UpdateTransaction,
} from '../validation/transaction.zod'
import {
findBudget,
isUserBudgetMember,
Expand Down
2 changes: 1 addition & 1 deletion apps/api/v1/services/user.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { CreateUser } from '@6pm/validation'
import prisma from '../../lib/prisma'
import type { CreateUser } from '../validation'

export async function findUserById(id: string) {
return await prisma.user.findUnique({
Expand Down
2 changes: 1 addition & 1 deletion apps/api/v1/services/wallet.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { CreateWallet, UpdateWallet } from '@6pm/validation'
import type { User, UserWalletAccount } from '@prisma/client'
import prisma from '../../lib/prisma'
import type { CreateWallet, UpdateWallet } from '../validation'

export async function findUserWallet({
user,
Expand Down
1 change: 1 addition & 0 deletions packages/validation/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# @6pm/validation
15 changes: 15 additions & 0 deletions packages/validation/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "@6pm/validation",
"version": "1.0.0",
"description": "Zod schemas",
"main": "src/index.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"zod": "^3.23.8"
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { BudgetPeriodType, BudgetType } from '@prisma/client'
import { z } from 'zod'

export const zCreateBudget = z.object({
name: z.string(),
description: z.string().optional(),
preferredCurrency: z.string(),
type: z.nativeEnum(BudgetType),
type: z.enum(['SPENDING', 'SAVING', 'INVESTING', 'DEBT']),
inviteeEmails: z.array(z.string().email()).optional(),
period: z.object({
type: z.nativeEnum(BudgetPeriodType),
type: z.enum(['MONTHLY', 'QUARTERLY', 'YEARLY', 'CUSTOM']),
amount: z.number().min(0),
startDate: z.date().optional(),
endDate: z.date().optional(),
Expand All @@ -20,9 +19,9 @@ export const zUpdateBudget = z.object({
name: z.string().optional(),
description: z.string().optional(),
preferredCurrency: z.string().optional(),
type: z.nativeEnum(BudgetType).optional(),
type: z.enum(['SPENDING', 'SAVING', 'INVESTING', 'DEBT']).optional(),
period: z.object({
type: z.nativeEnum(BudgetPeriodType).optional(),
type: z.enum(['MONTHLY', 'QUARTERLY', 'YEARLY', 'CUSTOM']).optional(),
amount: z.number().min(0).optional(),
startDate: z.date().optional(),
endDate: z.date().optional(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './auth.zod'
export * from './budget.zod'
export * from './user.zod'
export * from './wallet.zod'
export * from './transaction.zod'
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit cbf63ec

Please sign in to comment.