Skip to content

Commit

Permalink
migrate to valibot
Browse files Browse the repository at this point in the history
  • Loading branch information
wmalarski committed Aug 27, 2023
1 parent 4b629c4 commit 2dc8d02
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 69 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"tanstack",
"Topbar",
"trpc",
"undici"
"undici",
"valibot"
]
}
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
"eslint-plugin-solid": "^0.13.0",
"eslint-plugin-sort-keys-fix": "^1.1.2",
"eslint-plugin-tailwindcss": "^3.13.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"postcss": "^8.4.28",
"prettier": "^3.0.2",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"solid-start-node": "0.2.26",
"solid-start-vercel": "^0.3.4",
"tailwindcss": "^3.3.3",
Expand All @@ -39,11 +39,11 @@
"vite-plugin-solid": "^2.7.0"
},
"dependencies": {
"@notionhq/client": "2.2.11",
"@auth/core": "^0.12.0",
"@auth/solid-start": "^0.1.1",
"@kobalte/core": "^0.10.0",
"@kobalte/tailwindcss": "^0.7.0",
"@notionhq/client": "2.2.11",
"@solid-primitives/i18n": "^1.4.1",
"@solidjs/meta": "^0.28.6",
"@solidjs/router": "^0.8.3",
Expand All @@ -54,7 +54,7 @@
"solid-start": "0.3.4",
"tailwind-merge": "1.14.0",
"undici": "^5.23.0",
"zod": "^3.22.2"
"valibot": "^0.13.1"
},
"engines": {
"node": ">=18"
Expand Down
14 changes: 7 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions src/modules/invoices/InvoiceForm/InvoiceForm.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useI18n } from "@solid-primitives/i18n";
import { Show, createSignal, type Component, type JSX } from "solid-js";
import type { ZodIssue } from "zod";
import type * as v from "valibot";
import { Button } from "~/components/Button";
import { Card, CardBody } from "~/components/Card";
import { DatePicker } from "~/components/DatePicker";
Expand Down Expand Up @@ -41,7 +41,7 @@ const formDefault: InvoiceFormData = {

type FormItemContainerProps = {
children: JSX.Element;
error?: ZodIssue;
error?: v.Issue;
id: string;
label: string;
};
Expand All @@ -65,7 +65,7 @@ const FormItemContainer: Component<FormItemContainerProps> = (props) => {
};

type FormInputItemProps = TextFieldInputProps & {
error?: ZodIssue;
error?: v.Issue;
id: string;
isLoading: boolean;
label: string;
Expand Down Expand Up @@ -104,7 +104,7 @@ const SubHeading: Component<SubHeadingProps> = (props) => {
};

type Props = {
error?: Record<string, ZodIssue>;
error?: Record<string, v.Issue>;
id?: number;
initial?: InvoiceFormData;
isLoading: boolean;
Expand Down
16 changes: 8 additions & 8 deletions src/server/env.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import type { FetchEvent } from "solid-start";
import { z } from "zod";
import * as v from "valibot";

if (typeof window !== "undefined") {
throw new Error("SERVER ON CLIENT!");
}

const getEnvSchema = () => {
return z.object({
googleId: z.string(),
googleSecret: z.string(),
notionDatabase: z.string(),
notionKey: z.string(),
return v.object({
googleId: v.string(),
googleSecret: v.string(),
notionDatabase: v.string(),
notionKey: v.string(),
});
};

type ServerEnv = z.infer<ReturnType<typeof getEnvSchema>>;
type ServerEnv = v.Input<ReturnType<typeof getEnvSchema>>;

type ServerEnvArgs = Pick<FetchEvent, "env" | "locals">;

Expand All @@ -28,7 +28,7 @@ export const serverEnv = ({ env, locals }: ServerEnvArgs): ServerEnv => {

const envSchema = getEnvSchema();

const parsed = envSchema.parse({
const parsed = v.parse(envSchema, {
googleId: env.GOOGLE_ID,
googleSecret: env.GOOGLE_SECRET,
notionDatabase: env.NOTION_DATABASE,
Expand Down
104 changes: 59 additions & 45 deletions src/server/invoices/actions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import server$, { useRequest } from "solid-start/server";
import { z } from "zod";
import * as v from "valibot";
import {
createInvoice,
deleteInvoice,
Expand All @@ -8,17 +8,21 @@ import {
updateInvoice,
} from "./api";

const selectInvoiceArgs = z.object({
id: z.coerce.number(),
});
const selectInvoiceArgs = () => {
return v.object({
id: v.coerce(v.number(), Number),
});
};

export const selectInvoiceKey = (args: z.infer<typeof selectInvoiceArgs>) => {
export const selectInvoiceKey = (
args: v.Input<ReturnType<typeof selectInvoiceArgs>>,
) => {
return ["selectInvoice", args] as const;
};

export const selectInvoiceServerQuery = server$(
([, args]: ReturnType<typeof selectInvoiceKey>) => {
const parsed = selectInvoiceArgs.parse(args);
const parsed = v.parse(selectInvoiceArgs(), args);

const requestEvent = useRequest();

Expand All @@ -39,11 +43,15 @@ export const selectInvoiceServerQuery = server$(
},
);

const selectInvoicesArgs = z.object({
startCursor: z.string().optional(),
});
const selectInvoicesArgs = () => {
return v.object({
startCursor: v.optional(v.string()),
});
};

export const selectInvoicesKey = (args: z.infer<typeof selectInvoicesArgs>) => {
export const selectInvoicesKey = (
args: v.Input<ReturnType<typeof selectInvoicesArgs>>,
) => {
return ["selectInvoices", args] as const;
};

Expand All @@ -53,7 +61,7 @@ export const selectAllInvoicesKey = () => {

export const selectInvoicesServerQuery = server$(
async ([, args]: ReturnType<typeof selectInvoicesKey>) => {
const parsed = selectInvoicesArgs.parse(args);
const parsed = v.parse(selectInvoicesArgs(), args);

const requestEvent = useRequest();

Expand Down Expand Up @@ -86,37 +94,41 @@ export const selectInvoicesServerQuery = server$(
},
);

const invoiceSchema = z.object({
buyerAddress1: z.string(),
buyerAddress2: z.string(),
buyerName: z.string(),
buyerNip: z.string(),
city: z.string(),
date: z.string(),
invoiceTitle: z.string(),
notes: z.string(),
paymentAccount: z.string(),
paymentBank: z.string(),
paymentMethod: z.string(),
sellerAddress1: z.string(),
sellerAddress2: z.string(),
sellerName: z.string(),
sellerNip: z.string(),
serviceCount: z.coerce.number().min(0),
servicePayed: z.coerce.number().min(0),
servicePrice: z.coerce.number().min(0),
serviceTitle: z.string(),
serviceUnit: z.string(),
});

const updateInvoiceArgs = z.intersection(
invoiceSchema,
z.object({ id: z.coerce.number() }),
);
const invoiceSchema = () => {
return v.object({
buyerAddress1: v.string(),
buyerAddress2: v.string(),
buyerName: v.string(),
buyerNip: v.string(),
city: v.string(),
date: v.string(),
invoiceTitle: v.string(),
notes: v.string(),
paymentAccount: v.string(),
paymentBank: v.string(),
paymentMethod: v.string(),
sellerAddress1: v.string(),
sellerAddress2: v.string(),
sellerName: v.string(),
sellerNip: v.string(),
serviceCount: v.coerce(v.number([v.minValue(0)]), Number),
servicePayed: v.coerce(v.number([v.minValue(0)]), Number),
servicePrice: v.coerce(v.number([v.minValue(0)]), Number),
serviceTitle: v.string(),
serviceUnit: v.string(),
});
};

const updateInvoiceArgs = () => {
return v.merge([
invoiceSchema(),
v.object({ id: v.coerce(v.number(), Number) }),
]);
};

export const updateInvoiceServerMutation = server$(
async (data: z.infer<typeof updateInvoiceArgs>) => {
const parsed = updateInvoiceArgs.parse(data);
async (data: v.Input<ReturnType<typeof updateInvoiceArgs>>) => {
const parsed = v.parse(updateInvoiceArgs(), data);

// const user = await getUser(server$);

Expand All @@ -131,8 +143,8 @@ export const updateInvoiceServerMutation = server$(
);

export const insertInvoiceServerMutation = server$(
async (data: z.infer<typeof invoiceSchema>) => {
const parsed = invoiceSchema.parse(data);
async (data: v.Input<ReturnType<typeof invoiceSchema>>) => {
const parsed = v.parse(invoiceSchema(), data);

// const user = await getUser(server$);

Expand All @@ -142,11 +154,13 @@ export const insertInvoiceServerMutation = server$(
},
);

const deleteSchemaArgs = z.object({ id: z.coerce.number() });
const deleteSchemaArgs = () => {
return v.object({ id: v.coerce(v.number(), Number) });
};

export const deleteInvoiceServerMutation = server$(
async (data: z.infer<typeof deleteSchemaArgs>) => {
const parsed = deleteSchemaArgs.parse(data);
async (data: v.Input<ReturnType<typeof deleteSchemaArgs>>) => {
const parsed = v.parse(deleteSchemaArgs(), data);

await deleteInvoice({ event: server$, id: parsed.id });

Expand Down

0 comments on commit 2dc8d02

Please sign in to comment.