File tree Expand file tree Collapse file tree 1 file changed +22
-1
lines changed
question-service/src/models Expand file tree Collapse file tree 1 file changed +22
-1
lines changed Original file line number Diff line number Diff line change @@ -3,6 +3,27 @@ import { ObjectId } from 'mongodb';
3
3
4
4
// object id schema
5
5
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
+ } )
6
27
7
28
export const QuestionsSchema = z . object ( {
8
29
_id : objectIdSchema ,
@@ -13,7 +34,7 @@ export const QuestionsSchema = z.object({
13
34
tags : z . array ( z . string ( ) ) ,
14
35
title_slug : z . string ( ) ,
15
36
title : z . string ( ) ,
16
- pictures : z . array ( z . instanceof ( File ) ) . optional ( ) ,
37
+ pictures : z . array ( imageSchema ) . optional ( ) ,
17
38
} ) ;
18
39
19
40
export const UserQuestionsSchema = z . object ( {
You can’t perform that action at this time.
0 commit comments