fix: Organize src/lib/types folder and Zod schemas

This commit is contained in:
April Hall 2025-02-15 23:23:52 -05:00
parent cc49ad84fb
commit eec5fc94ea
Signed by: arithefirst
GPG Key ID: 4508A15C4DB91C5B
13 changed files with 81 additions and 75 deletions

View File

@ -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';

View File

@ -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';

View File

@ -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';

30
src/lib/types/account.ts Normal file
View File

@ -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;

8
src/lib/types/login.ts Normal file
View File

@ -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;

7
src/lib/types/misc.ts Normal file
View File

@ -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;

View File

@ -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;

26
src/lib/types/signup.ts Normal file
View File

@ -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;

View File

@ -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';

View File

@ -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';

View File

@ -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 }) {

View File

@ -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({

View File

@ -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({