All files / src/validations imageItem.ts

100% Statements 21/21
100% Branches 7/7
100% Functions 4/4
100% Lines 21/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 271x   1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x        
import { z } from 'zod';
 
//許容するMIMEタイプ
const ACCEPT_MIME_TYPES = ['image/png', 'image/jpeg', 'image/webp'];
//許容するファイルサイズ
const MAX_SIZE = 1024 * 1024 * 100; //100MB
 
const imageItemSchema = z.object({
  image: z
    .custom<FileList>()
    .refine((files) => 0 < files.length, {
      message: '画像ファイルの添付は必須です',
    })
    .refine((files) => 0 < files.length && files.length < 2, {
      message: '添付できる画像ファイルは1枚だけです',
    })
    .refine((files) => Array.from(files).every((file) => file.size < MAX_SIZE), {
      message: '添付できる画像ファイルは100MBまでです',
    })
    .refine((files) => Array.from(files).every((file) => ACCEPT_MIME_TYPES.includes(file.type)), {
      message: '添付できる画像ファイルは jpeg、jpg、png、webp です',
    }),
});
 
export { imageItemSchema };
export type ImageItemSchemaType = z.infer<typeof imageItemSchema>;