aboutsummaryrefslogtreecommitdiff
path: root/api/src/modules/one-time-expenses/one-time-expenses.schema.ts
blob: 9e32c8d8d36c338eaab612819e0a7482d1d27869 (plain)
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
import { z } from 'zod';

const CATEGORIES = [
  'moradia',
  'alimentação',
  'transporte',
  'saúde',
  'educação',
  'lazer',
  'serviços',
  'outros',
] as const;

export const categoryEnum = z.enum(CATEGORIES);
export const typeEnum = z.enum(['expense', 'income']);

export const createOneTimeExpenseSchema = z.object({
  name: z.string().min(1, 'Nome é obrigatório').max(100),
  amount: z.number().positive('Valor deve ser positivo'),
  category: categoryEnum.default('outros'),
  type: typeEnum.default('expense'),
  /** Data do gasto no formato YYYY-MM-DD. Default: hoje. */
  expense_date: z
    .string()
    .regex(/^\d{4}-\d{2}-\d{2}$/, 'Formato: YYYY-MM-DD')
    .optional(),
  notes: z.string().max(500).nullable().optional(),
});

export const updateOneTimeExpenseSchema = z.object({
  name: z.string().min(1).max(100).optional(),
  amount: z.number().positive().optional(),
  category: categoryEnum.optional(),
  type: typeEnum.optional(),
  expense_date: z.string().regex(/^\d{4}-\d{2}-\d{2}$/).optional(),
  notes: z.string().max(500).nullable().optional(),
});

export const oneTimeExpenseParamsSchema = z.object({
  userId: z.coerce.number().int().positive(),
  id: z.coerce.number().int().positive(),
});

export const oneTimeExpenseUserParamsSchema = z.object({
  userId: z.coerce.number().int().positive(),
});

export const oneTimeExpenseQuerySchema = z.object({
  month: z.coerce.number().int().min(1).max(12).optional(),
  year: z.coerce.number().int().min(2020).optional(),
});

export type CreateOneTimeExpenseInput = z.infer<typeof createOneTimeExpenseSchema>;
export type UpdateOneTimeExpenseInput = z.infer<typeof updateOneTimeExpenseSchema>;