All files / src/validations updateItem.ts

100% Statements 41/41
100% Branches 7/7
100% Functions 2/2
100% Lines 41/41

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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 471x   1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x        
import { z } from 'zod';
 
//時間の取得
const today = new Date();
//NaNをnumberとして扱えるようにする
const NaNSchema: z.ZodSchema<number> = z.any().refine(Number.isNaN);
 
const updateItemSchema = z.object({
  name: z.string().min(1, { message: '名前は、1文字以上入れてください。' }),
  visible_id: z
    .string()
    .min(4, { message: '物品IDは、4文字の英数字です。' })
    .max(4, { message: '物品IDは、4文字の英数字です。' })
    .regex(/^[A-Z0-9]+$/, {
      message: '半角英数字(英字は大文字)で入力してください',
    }),
  product_number: z.string(),
  description: z.string(),
  purchase_year: z
    .number()
    .or(NaNSchema)
    .refine((year) => Number.isNaN(year) || (year >= 2000 && year <= today.getFullYear() && Number.isInteger(year)), {
      message: `西暦 2000年から、西暦 ${today.getFullYear()}年までの西暦の数字4桁を入力してください。`,
    })
    .nullable(),
  purchase_price: z
    .number()
    .or(NaNSchema)
    .refine((price) => Number.isNaN(price) || (price >= 0 && Number.isInteger(price)), {
      message: `0円以上の金額を入力してください。`,
    })
    .nullable(),
  durability: z
    .number()
    .or(NaNSchema)
    .refine((price) => Number.isNaN(price) || (price >= 1 && Number.isInteger(price)), {
      message: `1年以上の年数を入力してください。`,
    })
    .nullable(),
  is_depreciation: z.boolean(),
  connector: z.custom<{ connector: string }[]>(),
  color: z.custom<{ color: string }[]>(),
});
 
export { updateItemSchema };
export type UpdateItemSchemaType = z.infer<typeof updateItemSchema>;