import { z } from 'zod'; export const USERNAME_MIN_LENGTH = 3; export const USERNAME_MAX_LENGTH = 30; /** Checked after lower-casing, so "Alice" is accepted and stored as "alice". */ export const USERNAME_PATTERN = /^[a-z0-9_.]+$/; export const NAME_MAX_LENGTH = 50; export const BIO_MAX_LENGTH = 190; /** A unique handle, e.g. for finding someone to add as a friend. */ export const usernameSchema = z .string() .trim() .toLowerCase() .min( USERNAME_MIN_LENGTH, `Username must be at least ${USERNAME_MIN_LENGTH} characters.`, ) .max( USERNAME_MAX_LENGTH, `Username must be at most ${USERNAME_MAX_LENGTH} characters.`, ) .regex( USERNAME_PATTERN, 'Username can only contain letters, numbers, underscores and dots.', ); /** The name shown next to someone's messages; not unique. */ export const displayNameSchema = z .string() .trim() .min(1, 'Name is required.') .max(NAME_MAX_LENGTH, `Name must be at most ${NAME_MAX_LENGTH} characters.`); /** Blank text clears the bio. */ export const bioSchema = z .string() .trim() .max(BIO_MAX_LENGTH, `Bio must be at most ${BIO_MAX_LENGTH} characters.`) .transform((bio) => (bio === '' ? null : bio)) .nullable(); /** A user id as issued by the auth system (not a UUID). */ export const userIdSchema = z.string().min(1).max(64); /** Enough of a user to show them in a list or next to a message. */ export const userSummarySchema = z.object({ id: z.string(), /** Null only for an account that has not picked one yet. */ username: z.string().nullable(), name: z.string(), image: z.string().nullable(), }); export type UserSummary = z.infer; /** What anyone signed in may see about a user. */ export const publicProfileSchema = z.object({ id: z.string(), username: z.string(), name: z.string(), bio: z.string().nullable(), image: z.string().nullable(), createdAt: z.iso.datetime(), }); export type PublicProfile = z.infer; /** The signed-in user's own profile. */ export const ownProfileSchema = publicProfileSchema.extend({ /** Null until the user picks one (accounts created before usernames). */ username: z.string().nullable(), email: z.string(), }); export type OwnProfile = z.infer; /** Body of PATCH /users/me; only the fields sent are changed. */ export const updateProfileSchema = z .object({ name: displayNameSchema, username: usernameSchema, bio: bioSchema, }) .partial(); export type UpdateProfile = z.infer;