Skip to content

Commit 58fe701

Browse files
committed
Update types.ts
1 parent 7a2df53 commit 58fe701

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

question-service/src/models/types.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,27 @@ import { ObjectId } from 'mongodb';
33

44
// object id schema
55
const objectIdSchema = z.instanceof(ObjectId);
6+
// image file schema
7+
const MAX_FILE_SIZE = 5000000
8+
const ACCEPTED_IMAGE_TYPES = [
9+
'image/jpeg',
10+
'image/jpg',
11+
'image/png',
12+
'image/webp',
13+
]
14+
15+
const imageSchema = z
16+
.any()
17+
// To not allow empty files
18+
.refine((files) => files?.length >= 1, { message: 'Image is required.' })
19+
// To not allow files other than images
20+
.refine((files) => ACCEPTED_IMAGE_TYPES.includes(files?.[0]?.type), {
21+
message: '.jpg, .jpeg, .png and .webp files are accepted.',
22+
})
23+
// To not allow files larger than 5MB
24+
.refine((files) => files?.[0]?.size <= MAX_FILE_SIZE, {
25+
message: `Max file size is 5MB.`,
26+
})
627

728
export const QuestionsSchema = z.object({
829
_id: objectIdSchema,
@@ -13,7 +34,7 @@ export const QuestionsSchema = z.object({
1334
tags: z.array(z.string()),
1435
title_slug: z.string(),
1536
title: z.string(),
16-
pictures: z.array(z.instanceof(File)).optional(),
37+
pictures: z.array(imageSchema).optional(),
1738
});
1839

1940
export const UserQuestionsSchema = z.object({

0 commit comments

Comments
 (0)