-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaward.zod.ts
165 lines (149 loc) · 4.3 KB
/
award.zod.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { ZodError, z } from 'zod'
import { NextFunction, Request, Response } from 'express'
import { BadRequestError } from '../middlewares'
import { parseAsync, ErrorMessageOptions } from 'zod-error'
import { validate as isUUID } from 'uuid'
const urlRegex = new RegExp(
'^((ft|htt)ps?:\\/\\/)?' +
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|' + // domain name
'((\\d{1,3}\\.){3}\\d{1,3}))' +
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*' +
'(\\?[;&a-z\\d%_.~+=-]*)?' +
'(\\#[-a-z\\d_]*)?$',
'i'
)
const charRegex = /^[a-zA-Z0-9\s]+$/
const CreateAwardDataSchema = z.object({
title: z
.string()
.min(3)
.regex(charRegex, { message: 'Title cannot contain special characters' }),
year: z.string().min(3),
presented_by: z
.string()
.min(3, { message: 'field cannot be empty' })
.regex(charRegex, { message: 'Title cannot contain special characters' }),
url: z
.string()
.min(3, { message: 'field cannot be empty' })
.optional()
.refine(
(value) => {
if (value) {
return urlRegex.test(value)
}
return true
},
{ message: 'Invalid URL format' }
),
userId: z
.string()
.min(3)
.refine((value) => isUUID(value), {
message: 'userId has to be a valid UUID',
}),
})
export const UpdateAwardDataSchema = z.object({
title: z
.string()
.min(3)
.regex(charRegex, { message: 'Title cannot contain special characters' })
.optional(),
year: z.string().min(3),
presented_by: z
.string()
.min(3, { message: 'field cannot be empty' })
.regex(charRegex, { message: 'Title cannot contain special characters' })
.optional(),
url: z
.string()
.min(3, { message: 'field cannot be empty' })
.optional()
.refine(
(value) => {
if (value) {
return urlRegex.test(value)
}
return true
},
{ message: 'Invalid URL format' }
),
userId: z.string().refine((value) => isUUID(value), {
message: 'userId has to be a valid UUID',
}),
})
// Custom function to validate date strings in "yyyy" format
function validateDateYYYY(dateString: string) {
const datePattern = /^\d{4}$/
return datePattern.test(dateString)
}
const options: ErrorMessageOptions = {
delimiter: {
error: ' 🔥 ',
},
transform: ({ errorMessage, index }) =>
`Error #${index + 1}: ${errorMessage}`,
}
async function validateCreateAwardData(
req: Request,
res: Response,
next: NextFunction
) {
try {
const data = req.body
// Validate date strings in "yy-mm-dd" format
if (data.year && !validateDateYYYY(data.year)) {
throw new BadRequestError(
"Invalid 'year' date format, it must be 'yyyy' "
)
}
// Retrieve the "userId" from request parameters
const userId = req.params.userId
// Validate the rest of the data against the schema
const result = await parseAsync(CreateAwardDataSchema, {
...data,
userId,
options,
})
// Store the validated data in the request object if needed
const validatedData = result
console.log(validatedData)
next() // Continue to the next middleware or route handler
} catch (error) {
const err = new BadRequestError(error.message)
// return res.status(400).json({
// success: false,
// message: error.message,
// })
// let message = 'Invalid input'
const errorMessage = error.message.split(':').pop().trim()
console.log(errorMessage)
next(new BadRequestError(errorMessage))
}
}
async function validateUpdateAwardData(
req: Request,
res: Response,
next: NextFunction
) {
try {
const data = req.body
// Validate date strings in "yyyy" format
if (data.year && !validateDateYYYY(data.year)) {
const err = new BadRequestError(
"Invalid 'year' date format, it must be 'yyyy'"
)
return res.status(err.statusCode).json({ error: err.message })
}
// Validate the data against the schema
const result = await parseAsync(UpdateAwardDataSchema, data, options)
const validatedData = result
console.log(validatedData)
next()
} catch (error) {
const err = new BadRequestError(error.message)
console.error(err.message)
res.status(err.statusCode).json({ error: err.message })
}
}
export { validateCreateAwardData, validateUpdateAwardData }