Skip to content

Commit 1284d37

Browse files
committed
chore: update deps
1 parent 85f2937 commit 1284d37

21 files changed

+649
-294
lines changed

app/config/env.config.ts

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,44 +8,37 @@ const envSchema = z.object({
88
CLOUDINARY_API_KEY: z.string(),
99
CLOUDINARY_API_SECRET: z.string(),
1010
CLOUDINARY_CLOUD_NAME: z.string(),
11-
DATABASE_URL: z.string().url(),
12-
FRONTEND_URL: z.string().url(),
11+
DATABASE_URL: z.url(),
12+
FRONTEND_URL: z.url(),
1313
GOOGLE_CLIENT_ID: z.string(),
1414
GOOGLE_CLIENT_SECRET: z.string(),
15-
GOOGLE_REDIRECT_URI: z.string().url(),
15+
GOOGLE_REDIRECT_URI: z.url(),
1616
REDIS_USERNAME: z.string(),
1717
REDIS_PASSWORD: z.string(),
1818
REDIS_HOST: z.string(),
19-
REDIS_PORT: z.preprocess(
20-
v => (v ? v : undefined),
21-
z.coerce.number().int().positive()
22-
),
19+
REDIS_PORT: z.coerce.number().int().positive().min(1000).max(65535),
2320
NODE_ENV: z.enum(['development', 'production']),
24-
COOKIE_HTTP_ONLY: z
25-
.enum(['true', 'false'])
26-
.transform(value => value === 'true'),
27-
COOKIE_SECURE: z.enum(['true', 'false']).transform(value => value === 'true'),
21+
COOKIE_HTTP_ONLY: z.stringbool(),
22+
COOKIE_SECURE: z.stringbool(),
2823
COOKIE_SAME_SITE: z.enum(['lax', 'strict', 'none']),
2924
COOKIE_DOMAIN: z.string(),
3025
ACCESS_JWT_SECRET: z.string().transform(v => new TextEncoder().encode(v)),
3126
REFRESH_JWT_SECRET: z.string().transform(v => new TextEncoder().encode(v)),
32-
PORT: z.preprocess(
33-
v => (v ? v : undefined),
34-
z.coerce.number().int().positive()
35-
),
27+
PORT: z.coerce.number().int().positive().min(1000).max(65535),
3628
API_PREFIX: z.string(),
3729
ALLOWED_ORIGINS: z
3830
.string()
3931
.transform(v => v.split(','))
40-
.pipe(z.array(z.string().url())),
32+
.pipe(z.array(z.url())),
4133
EMAIL_HOST: z.string(),
42-
EMAIL_PORT: z
43-
.number({ coerce: true })
44-
.refine(v => availableEmailPorts.includes(v), {
45-
message: `Email port must be one of the following: ${availableEmailPorts.join(', ')}`
46-
}),
47-
EMAIL_USER: z.string().email(),
48-
EMAIL_RECEIVER: z.string().email(),
34+
EMAIL_PORT: z.coerce
35+
.number()
36+
.refine(
37+
v => availableEmailPorts.includes(v),
38+
`Email port must be one of the following: ${availableEmailPorts.join(', ')}`
39+
),
40+
EMAIL_USER: z.email(),
41+
EMAIL_RECEIVER: z.email(),
4942
EMAIL_PASSWORD: z.string(),
5043
ACCESS_JWT_EXPIRES_IN: z.string(),
5144
REFRESH_JWT_EXPIRES_IN: z.string(),

app/controllers/auth.controller.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
import crypto from 'crypto'
22
import type { GoogleCodeSchema, SigninSchema, SignupSchema } from '@/schemas'
3-
import type { JwtPayload } from '@/types'
3+
import type { JwtPayload, TypedRequestBody, TypedRequestQuery } from '@/types'
44
import type { NextFunction, Request, Response } from 'express'
5-
import type {
6-
TypedRequestBody,
7-
TypedRequestQuery
8-
} from 'zod-express-middleware'
95

106
import { prisma } from '@/prisma'
117
import { hash, verify } from 'argon2'

app/controllers/board.controller.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import type {
33
BoardParamsSchema,
44
EditBoardSchema
55
} from '@/schemas'
6-
import type { NextFunction, Request, Response } from 'express'
7-
import type { ZodType } from 'zod'
86
import type {
97
TypedRequest,
108
TypedRequestBody,
119
TypedRequestParams
12-
} from 'zod-express-middleware'
10+
} from '@/types'
11+
import type { NextFunction, Request, Response } from 'express'
12+
import type { ZodType } from 'zod'
1313

1414
import { prisma } from '@/prisma'
1515
import { NotFound } from 'http-errors'

app/controllers/card.controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import type {
55
EditCardSchema,
66
UpdateCardOrderSchema
77
} from '@/schemas'
8+
import type { TypedRequest, TypedRequestParams } from '@/types'
89
import type { NextFunction, Response } from 'express'
910
import type { ZodType } from 'zod'
10-
import type { TypedRequest, TypedRequestParams } from 'zod-express-middleware'
1111

1212
import { prisma } from '@/prisma'
1313
import { BadRequest, NotFound } from 'http-errors'

app/controllers/column.controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import type {
55
EditColumnSchema,
66
UpdateColumnOrderSchema
77
} from '@/schemas'
8+
import type { TypedRequest, TypedRequestParams } from '@/types'
89
import type { NextFunction, Response } from 'express'
910
import type { ZodType } from 'zod'
10-
import type { TypedRequest, TypedRequestParams } from 'zod-express-middleware'
1111

1212
import { prisma } from '@/prisma'
1313
import { BadRequest, NotFound } from 'http-errors'

app/controllers/user.controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { EditUserSchema, NeedHelpSchema } from '@/schemas'
2+
import type { TypedRequestBody } from '@/types'
23
import type { User } from '@prisma/client'
34
import type { NextFunction, Request, Response } from 'express'
45
import type { Options } from 'nodemailer/lib/mailer'
5-
import type { TypedRequestBody } from 'zod-express-middleware'
66

77
import { prisma } from '@/prisma'
88
import { hash } from 'argon2'

app/middlewares/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export { authenticate } from './authenticate'
22
export { upload } from './multer'
33
export { globalLimiter } from './limiter'
4+
export { validateRequest } from './validate-request'
45
export { notFoundHandler, globalErrorHandler } from './errorHandler'
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import type { RequestHandler } from 'express'
2+
import type * as z from 'zod'
3+
4+
import createHttpError from 'http-errors'
5+
6+
type RequestValidation<P, Q, B> = {
7+
params?: z.ZodType<P>
8+
query?: z.ZodType<Q>
9+
body?: z.ZodType<B>
10+
}
11+
12+
export const validateRequest: <P, Q, B>(
13+
schemas: RequestValidation<P, Q, B>
14+
) => RequestHandler<P, unknown, B, Q> =
15+
({ params, query, body }) =>
16+
(req, _, next) => {
17+
const errors: Record<string, string> = {}
18+
19+
if (params) {
20+
const parsed = params.safeParse(req.params)
21+
22+
if (!parsed.success) {
23+
parsed.error.issues.forEach(({ path, message }) => {
24+
const errorPath = `${path.join('.')}`
25+
errors[errorPath] = message
26+
})
27+
}
28+
}
29+
30+
if (query) {
31+
const parsed = query.safeParse(req.query)
32+
33+
if (!parsed.success) {
34+
parsed.error.issues.forEach(({ path, message }) => {
35+
const errorPath = `${path.join('.')}`
36+
errors[errorPath] = message
37+
})
38+
}
39+
}
40+
41+
if (body) {
42+
const parsed = body.safeParse(req.body)
43+
44+
if (!parsed.success) {
45+
parsed.error.issues.forEach(({ path, message }) => {
46+
const errorPath = `${path.join('.')}`
47+
errors[errorPath] = message
48+
})
49+
}
50+
}
51+
52+
if (Object.keys(errors).length > 0) {
53+
return next(createHttpError(400, { message: errors }))
54+
}
55+
56+
return next()
57+
}

app/routes/api/auth.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { Router } from 'express'
2-
import { validateRequest } from 'zod-express-middleware'
32

43
import { authController } from '@/controllers'
54

65
import { authenticate } from '@/middlewares'
6+
import { validateRequest } from '@/middlewares/validate-request'
77

88
import { GoogleCodeSchema, SigninSchema, SignupSchema } from '@/schemas'
99

app/routes/api/board.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { Router } from 'express'
2-
import { validateRequest } from 'zod-express-middleware'
32

43
import { boardController } from '@/controllers'
54

6-
import { authenticate } from '@/middlewares'
5+
import { authenticate, validateRequest } from '@/middlewares'
76

87
import { AddBoardSchema, BoardParamsSchema, EditBoardSchema } from '@/schemas'
98

0 commit comments

Comments
 (0)