Files
Attendance/apps/web-app/src/features/auth/schemas/auth.schema.ts
T

54 lines
1.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { z } from "zod";
import { emailSchema, passwordSchema } from "../../../lib/validation";
/**
* Auth form schemas — validated with TanStack Form
* (`@tanstack/react-form` + `zodValidator` from `@tanstack/zod-form-adapter`).
*
* Rules mirror the server-side config in `apps/worker/src/lib/auth/auth-options.ts`
* (email/password + username plugin):
* - password minimum 8 chars (`minPasswordLength: 8`)
* - username 3–30 chars, alphanumeric + underscores (username plugin default)
*/
export const firstNameSchema = z
.string()
.trim()
.min(1, "First name is required")
.max(50, "First name is too long");
export const lastNameSchema = z
.string()
.trim()
.min(1, "Last name is required")
.max(50, "Last name is too long");
export const usernameSchema = z
.string()
.trim()
.refine((value) => value === "" || /^[a-zA-Z0-9_]{3,30}$/.test(value), {
message: "3–30 characters — letters, numbers and underscores only",
});
export const loginSchema = z.object({
email: emailSchema,
password: passwordSchema,
});
export const signupSchema = z
.object({
firstName: firstNameSchema,
lastName: lastNameSchema,
email: emailSchema,
username: usernameSchema,
password: passwordSchema,
confirmPassword: z.string(),
})
.refine((data) => data.password === data.confirmPassword, {
message: "Passwords do not match",
path: ["confirmPassword"],
});
export type SignupInput = z.infer<typeof signupSchema>;
export type LoginInput = z.infer<typeof loginSchema>;