Skip to content

Commit

Permalink
move validators to their own file
Browse files Browse the repository at this point in the history
  • Loading branch information
willianba committed Jul 4, 2024
1 parent 1e79835 commit 72d1878
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 64 deletions.
21 changes: 4 additions & 17 deletions routes/api/expenses/[id].ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,12 @@ import ExpenseService, {
} from "@/db/models/expense.ts";
import { SignedInState } from "@/plugins/session.ts";
import logger from "@/utils/logger.ts";
import { z } from "zod";
import PaymentMethodService from "@/db/models/payment-method.ts";
import CategoryService from "@/db/models/category.ts";

const UpdateExpenseSchema = z.object({
name: z.string(),
paymentDate: z.string().date(),
paymentMethod: z.string(),
paymentCategory: z.string(),
price: z.string().optional(),
propagate: z
.string()
.optional()
.transform((v) => v === "true"),
});

const DeleteExpenseSchema = z.object({
propagate: z.boolean().optional().default(false),
});
import {
DeleteExpenseSchema,
UpdateExpenseSchema,
} from "@/utils/expenses/validators.ts";

export const handler: Handlers<ExpenseWithoutUser, SignedInState> = {
async PUT(req, ctx) {
Expand Down
7 changes: 1 addition & 6 deletions routes/api/expenses/date.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { Handlers } from "$fresh/server.ts";
import ExpenseService, { Expense } from "@/db/models/expense.ts";
import { SignedInState } from "@/plugins/session.ts";
import { z } from "zod";

const ExpensesByMonthSchema = z.object({
year: z.string(),
month: z.string().optional(),
});
import { ExpensesByMonthSchema } from "@/utils/expenses/validators.ts";

export const handler: Handlers<Expense, SignedInState> = {
async GET(_req, ctx) {
Expand Down
22 changes: 1 addition & 21 deletions routes/api/expenses/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Handlers } from "$fresh/server.ts";
import { PaymentType } from "@/utils/constants.ts";
import { z } from "zod";
import ExpenseService, {
ExpenseWithoutUser,
Expand All @@ -9,26 +8,7 @@ import { SignedInState } from "@/plugins/session.ts";
import logger from "@/utils/logger.ts";
import ExpenseInputFactory from "@/utils/expenses/factory.ts";
import { stripDate, today } from "@/utils/date.ts";

const CreateExpenseSchema = z
.object({
name: z.string(),
price: z.string(),
paymentDate: z.string().date(),
paymentMethod: z.string(),
paymentCategory: z.string(),
paymentType: z.nativeEnum(PaymentType),
installments: z.string().optional(),
})
.refine(
(schema) => {
if (schema.paymentType === PaymentType.OVER_TIME) {
return schema.installments !== undefined;
}
return true;
},
{ message: "Installments are required for payments over time" },
);
import { CreateExpenseSchema } from "@/utils/expenses/validators.ts";

export type CreateExpenseData = z.infer<typeof CreateExpenseSchema>;

Expand Down
8 changes: 1 addition & 7 deletions routes/api/income/[id].ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import { Handlers } from "$fresh/server.ts";
import { SignedInState } from "@/plugins/session.ts";
import logger from "@/utils/logger.ts";
import { z } from "zod";
import IncomeService, {
RawIncome,
UpdateIncomeInput,
} from "@/db/models/income.ts";

const UpdateIncomeSchema = z.object({
source: z.string(),
date: z.string().date(),
price: z.string(),
});
import { UpdateIncomeSchema } from "@/utils/income/validators.ts";

export const handler: Handlers<RawIncome, SignedInState> = {
async PUT(req, ctx) {
Expand Down
7 changes: 1 addition & 6 deletions routes/api/income/date.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { Handlers } from "$fresh/server.ts";
import { SignedInState } from "@/plugins/session.ts";
import { z } from "zod";
import IncomeService, { Income } from "@/db/models/income.ts";

const IncomeByDateSchema = z.object({
year: z.string(),
month: z.string().optional(),
});
import { IncomeByDateSchema } from "@/utils/income/validators.ts";

export const handler: Handlers<Income, SignedInState> = {
async GET(_req, ctx) {
Expand Down
8 changes: 1 addition & 7 deletions routes/api/income/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import { Handlers } from "$fresh/server.ts";
import { SignedInState } from "@/plugins/session.ts";
import IncomeService, { Income } from "@/db/models/income.ts";
import { z } from "zod";
import logger from "@/utils/logger.ts";

const CreateIncomeSchema = z.object({
source: z.string(),
date: z.string().date(),
price: z.string(),
});
import { CreateIncomeSchema } from "@/utils/income/validators.ts";

export const handler: Handlers<Income, SignedInState> = {
async POST(req, ctx) {
Expand Down
43 changes: 43 additions & 0 deletions utils/expenses/validators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { z } from "zod";
import { PaymentType } from "@/utils/constants.ts";

export const ExpensesByMonthSchema = z.object({
year: z.string(),
month: z.string().optional(),
});

export const CreateExpenseSchema = z
.object({
name: z.string(),
price: z.string(),
paymentDate: z.string().date(),
paymentMethod: z.string(),
paymentCategory: z.string(),
paymentType: z.nativeEnum(PaymentType),
installments: z.string().optional(),
})
.refine(
(schema) => {
if (schema.paymentType === PaymentType.OVER_TIME) {
return schema.installments !== undefined;
}
return true;
},
{ message: "Installments are required for payments over time" },
);

export const UpdateExpenseSchema = z.object({
name: z.string(),
paymentDate: z.string().date(),
paymentMethod: z.string(),
paymentCategory: z.string(),
price: z.string().optional(),
propagate: z
.string()
.optional()
.transform((v) => v === "true"),
});

export const DeleteExpenseSchema = z.object({
propagate: z.boolean().optional().default(false),
});
18 changes: 18 additions & 0 deletions utils/income/validators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { z } from "zod";

export const UpdateIncomeSchema = z.object({
source: z.string(),
date: z.string().date(),
price: z.string(),
});

export const IncomeByDateSchema = z.object({
year: z.string(),
month: z.string().optional(),
});

export const CreateIncomeSchema = z.object({
source: z.string(),
date: z.string().date(),
price: z.string(),
});

0 comments on commit 72d1878

Please sign in to comment.