From eec5fc94ea0590c67b398908a10ea4a4d6d8dc10 Mon Sep 17 00:00:00 2001 From: April Hall Date: Sat, 15 Feb 2025 23:23:52 -0500 Subject: [PATCH] fix: Organize `src/lib/types` folder and Zod schemas --- src/lib/components/forms/channelDialog.svelte | 2 +- .../components/forms/updatePassword.svelte | 2 +- .../components/forms/updateUsername.svelte | 2 +- src/lib/types/account.ts | 30 +++++++++ src/lib/types/login.ts | 8 +++ src/lib/types/misc.ts | 7 ++ src/lib/types/schema.ts | 65 ------------------- src/lib/types/signup.ts | 26 ++++++++ src/routes/(main)/+layout.server.ts | 2 +- src/routes/(main)/+page.server.ts | 2 +- src/routes/(main)/account/+page.server.ts | 2 +- src/routes/login/+page.server.ts | 4 +- src/routes/signup/+page.server.ts | 4 +- 13 files changed, 81 insertions(+), 75 deletions(-) create mode 100644 src/lib/types/account.ts create mode 100644 src/lib/types/login.ts create mode 100644 src/lib/types/misc.ts delete mode 100644 src/lib/types/schema.ts create mode 100644 src/lib/types/signup.ts diff --git a/src/lib/components/forms/channelDialog.svelte b/src/lib/components/forms/channelDialog.svelte index 75be7b0..995d1a4 100644 --- a/src/lib/components/forms/channelDialog.svelte +++ b/src/lib/components/forms/channelDialog.svelte @@ -3,7 +3,7 @@ import * as Dialog from '$lib/components/ui/dialog/index.js'; import { Input } from '$lib/components/ui/input/index.js'; import { Label } from '$lib/components/ui/label/index'; - import type { NewChannelSchema } from '$lib/types/schema'; + import type { NewChannelSchema } from '$lib/types/misc'; import type { Infer, SuperValidated } from 'sveltekit-superforms'; import { superForm } from 'sveltekit-superforms'; diff --git a/src/lib/components/forms/updatePassword.svelte b/src/lib/components/forms/updatePassword.svelte index 25a6f0c..25614ce 100644 --- a/src/lib/components/forms/updatePassword.svelte +++ b/src/lib/components/forms/updatePassword.svelte @@ -2,7 +2,7 @@ import { Button } from '$lib/components/ui/button/index'; import { Input } from '$lib/components/ui/input/index'; import { Label } from '$lib/components/ui/label/index'; - import type { ChangePasswordSchema } from '$lib/types/schema'; + import type { ChangePasswordSchema } from '$lib/types/account'; import type { Infer, SuperValidated } from 'sveltekit-superforms'; import { superForm } from 'sveltekit-superforms'; diff --git a/src/lib/components/forms/updateUsername.svelte b/src/lib/components/forms/updateUsername.svelte index 9974eea..37b3a6b 100644 --- a/src/lib/components/forms/updateUsername.svelte +++ b/src/lib/components/forms/updateUsername.svelte @@ -2,7 +2,7 @@ import { Button } from '$lib/components/ui/button/index'; import { Input } from '$lib/components/ui/input/index'; import { Label } from '$lib/components/ui/label/index'; - import type { ChangeUsernameSchema } from '$lib/types/schema'; + import type { ChangeUsernameSchema } from '$lib/types/account'; import type { Infer, SuperValidated } from 'sveltekit-superforms'; import { superForm } from 'sveltekit-superforms'; diff --git a/src/lib/types/account.ts b/src/lib/types/account.ts new file mode 100644 index 0000000..2c1e2c6 --- /dev/null +++ b/src/lib/types/account.ts @@ -0,0 +1,30 @@ +import { z } from 'zod'; + +export const changePasswordSchema = z + .object({ + currentPassword: z.string().nonempty('Password must not be empty.'), + newPassword: z + .string() + .min(8, 'New password must be at least 8 characters.') + .regex(/(?=.*[A-Z])/gm, 'New password must contain at uppercase letter.') + .regex(/(?=.*[a-z])/gm, 'New password must contain at lowercase letter.') + .regex(/(?=.*\d)/gm, 'New password must contain at least one number.') + .regex(/(?=.*\W)/gm, 'New password must contain at least one special character'), + }) + .refine((schema) => schema.currentPassword !== schema.newPassword, { + message: 'New password cannot be the same as old password.', + path: ['newPassword'], + }); + +export const changeUsernameSchema = z.object({ + username: z + .string() + .min(3, 'Username must be at least 3 characters.') + .max(15, 'Username must be no more than 15 characters.') + .regex(/^(?![A-Z])/gm, 'Username cannot contain uppercase letters') + .regex(/^(?=[a-z0-9-_]+$)/gm, 'Username cannot contain special characters'), + password: z.string().nonempty('Password must not be empty.'), +}); + +export type ChangePasswordSchema = typeof changePasswordSchema; +export type ChangeUsernameSchema = typeof changeUsernameSchema; diff --git a/src/lib/types/login.ts b/src/lib/types/login.ts new file mode 100644 index 0000000..1dafabe --- /dev/null +++ b/src/lib/types/login.ts @@ -0,0 +1,8 @@ +import { z } from 'zod'; + +export const loginSchema = z.object({ + email: z.string().nonempty('An email is required').email('Please enter a valid email.'), + password: z.string().nonempty('Password must not be empty.'), +}); + +export type LogInSchema = typeof loginSchema; diff --git a/src/lib/types/misc.ts b/src/lib/types/misc.ts new file mode 100644 index 0000000..cb6e77b --- /dev/null +++ b/src/lib/types/misc.ts @@ -0,0 +1,7 @@ +import { z } from 'zod'; + +export const newChannelSchema = z.object({ + channelName: z.string().min(1, 'Channel name is required'), +}); + +export type NewChannelSchema = typeof newChannelSchema; diff --git a/src/lib/types/schema.ts b/src/lib/types/schema.ts deleted file mode 100644 index 434fe94..0000000 --- a/src/lib/types/schema.ts +++ /dev/null @@ -1,65 +0,0 @@ -import { z } from 'zod'; - -export const newChannelSchema = z.object({ - channelName: z.string().min(1, 'Channel name is required'), -}); - -export const signupSchema = z - .object({ - email: z.string().nonempty('An email is required').email('Please enter a valid email.'), - username: z - .string() - .min(3, 'Username must be at least 3 characters.') - .max(15, 'Username must be no more than 15 characters.') - .regex(/^(?![A-Z])/gm, 'Username cannot contain uppercase letters') - .regex(/^(?=[a-z0-9-_]+$)/gm, 'Username cannot contain special characters'), - password: z - .string() - .min(8, 'Password must be at least 8 characters.') - .regex(/(?=.*[A-Z])/gm, 'Password must contain at uppercase letter.') - .regex(/(?=.*[a-z])/gm, 'Password must contain at lowercase letter.') - .regex(/(?=.*\d)/gm, 'Password must contain at least one number.') - .regex(/(?=.*\W)/gm, 'Password must contain at least one special character'), - verify: z.string().nonempty('Passwords do not match.'), - }) - .refine((schema) => schema.verify === schema.password, { - message: "Passwords don't match", - path: ['verify'], - }); - -export const loginSchema = z.object({ - email: z.string().nonempty('An email is required').email('Please enter a valid email.'), - password: z.string().nonempty('Password must not be empty.'), -}); - -export const changePasswordSchema = z - .object({ - currentPassword: z.string().nonempty('Password must not be empty.'), - newPassword: z - .string() - .min(8, 'New password must be at least 8 characters.') - .regex(/(?=.*[A-Z])/gm, 'New password must contain at uppercase letter.') - .regex(/(?=.*[a-z])/gm, 'New password must contain at lowercase letter.') - .regex(/(?=.*\d)/gm, 'New password must contain at least one number.') - .regex(/(?=.*\W)/gm, 'New password must contain at least one special character'), - }) - .refine((schema) => schema.currentPassword !== schema.newPassword, { - message: 'New password cannot be the same as old password.', - path: ['newPassword'], - }); - -export const changeUsernameSchema = z.object({ - username: z - .string() - .min(3, 'Username must be at least 3 characters.') - .max(15, 'Username must be no more than 15 characters.') - .regex(/^(?![A-Z])/gm, 'Username cannot contain uppercase letters') - .regex(/^(?=[a-z0-9-_]+$)/gm, 'Username cannot contain special characters'), - password: z.string().nonempty('Password must not be empty.'), -}); - -export type ChangePasswordSchema = typeof changePasswordSchema; -export type ChangeUsernameSchema = typeof changeUsernameSchema; -export type NewChannelSchema = typeof newChannelSchema; -export type SignUpSchema = typeof signupSchema; -export type LogInSchema = typeof loginSchema; diff --git a/src/lib/types/signup.ts b/src/lib/types/signup.ts new file mode 100644 index 0000000..f1acaed --- /dev/null +++ b/src/lib/types/signup.ts @@ -0,0 +1,26 @@ +import { z } from 'zod'; + +export const signupSchema = z + .object({ + email: z.string().nonempty('An email is required').email('Please enter a valid email.'), + username: z + .string() + .min(3, 'Username must be at least 3 characters.') + .max(15, 'Username must be no more than 15 characters.') + .regex(/^(?![A-Z])/gm, 'Username cannot contain uppercase letters') + .regex(/^(?=[a-z0-9-_]+$)/gm, 'Username cannot contain special characters'), + password: z + .string() + .min(8, 'Password must be at least 8 characters.') + .regex(/(?=.*[A-Z])/gm, 'Password must contain at uppercase letter.') + .regex(/(?=.*[a-z])/gm, 'Password must contain at lowercase letter.') + .regex(/(?=.*\d)/gm, 'Password must contain at least one number.') + .regex(/(?=.*\W)/gm, 'Password must contain at least one special character'), + verify: z.string().nonempty('Passwords do not match.'), + }) + .refine((schema) => schema.verify === schema.password, { + message: "Passwords don't match", + path: ['verify'], + }); + +export type SignUpSchema = typeof signupSchema; diff --git a/src/routes/(main)/+layout.server.ts b/src/routes/(main)/+layout.server.ts index c8e2cc5..7a9516a 100644 --- a/src/routes/(main)/+layout.server.ts +++ b/src/routes/(main)/+layout.server.ts @@ -1,6 +1,6 @@ import { db } from '$lib/server/db'; import { auth } from '$lib/server/db/auth'; -import { newChannelSchema } from '$lib/types/schema'; +import { newChannelSchema } from '$lib/types/misc'; import { superValidate } from 'sveltekit-superforms'; import { zod } from 'sveltekit-superforms/adapters'; diff --git a/src/routes/(main)/+page.server.ts b/src/routes/(main)/+page.server.ts index 21034b7..606d632 100644 --- a/src/routes/(main)/+page.server.ts +++ b/src/routes/(main)/+page.server.ts @@ -1,6 +1,6 @@ import { db } from '$lib/server/db'; import { auth } from '$lib/server/db/auth'; -import { newChannelSchema } from '$lib/types/schema'; +import { newChannelSchema } from '$lib/types/misc'; import { fail, redirect } from '@sveltejs/kit'; import { message, setError, superValidate } from 'sveltekit-superforms'; import { zod } from 'sveltekit-superforms/adapters'; diff --git a/src/routes/(main)/account/+page.server.ts b/src/routes/(main)/account/+page.server.ts index 575c534..8406727 100644 --- a/src/routes/(main)/account/+page.server.ts +++ b/src/routes/(main)/account/+page.server.ts @@ -3,7 +3,7 @@ import type { Actions } from '@sveltejs/kit'; import { fail, message, setError, superValidate } from 'sveltekit-superforms'; import { zod } from 'sveltekit-superforms/adapters'; import { auth } from '$lib/server/db/auth'; -import { changeUsernameSchema, changePasswordSchema } from '$lib/types/schema.js'; +import { changeUsernameSchema, changePasswordSchema } from '$lib/types/account'; import type { APIError } from 'better-auth/api'; export async function load({ request }) { diff --git a/src/routes/login/+page.server.ts b/src/routes/login/+page.server.ts index 135b165..6e224c4 100644 --- a/src/routes/login/+page.server.ts +++ b/src/routes/login/+page.server.ts @@ -1,11 +1,11 @@ import { dev } from '$app/environment'; import { auth } from '$lib/server/db/auth'; -import { loginSchema } from '$lib/types/schema'; +import { loginSchema } from '$lib/types/login'; import { redirect } from '@sveltejs/kit'; +import type { APIError } from 'better-auth/api'; import { fail, message, setError, superValidate } from 'sveltekit-superforms'; import { zod } from 'sveltekit-superforms/adapters'; import type { Actions } from './$types'; -import type { APIError } from 'better-auth/api'; export async function load({ request }) { const session = await auth.api.getSession({ diff --git a/src/routes/signup/+page.server.ts b/src/routes/signup/+page.server.ts index 43a07e4..bcb1826 100644 --- a/src/routes/signup/+page.server.ts +++ b/src/routes/signup/+page.server.ts @@ -1,11 +1,11 @@ import { dev } from '$app/environment'; import { auth } from '$lib/server/db/auth'; -import { signupSchema } from '$lib/types/schema'; +import { signupSchema } from '$lib/types/signup'; import { redirect } from '@sveltejs/kit'; +import type { APIError } from 'better-auth/api'; import { fail, message, setError, superValidate } from 'sveltekit-superforms'; import { zod } from 'sveltekit-superforms/adapters'; import type { Actions } from './$types'; -import type { APIError } from 'better-auth/api'; export async function load({ request }) { const session = await auth.api.getSession({