All files / src/lib/validation/schemas user.ts

100% Statements 92/92
100% Branches 0/0
100% Functions 0/0
100% Lines 92/92

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 931x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
/**
 * User Validation Schemas
 *
 * Schemas for user profile, preferences, and account management.
 */
 
import { z } from 'zod';
import { emailSchema, nameSchema, phoneSchema, fullAddressSchema } from '../schemas';
 
// ============================================================================
// PROFILE
// ============================================================================
 
/**
 * Update user profile schema
 */
export const updateProfileSchema = z.object({
  firstName: nameSchema.optional(),
  lastName: nameSchema.optional(),
  phone: phoneSchema.optional().or(z.literal('')),
  avatar: z.string().url().optional().or(z.literal('')),
  bio: z.string().max(500, 'Bio must be less than 500 characters').optional(),
});
 
export type UpdateProfileInput = z.infer<typeof updateProfileSchema>;
 
/**
 * Full profile schema (for display)
 */
export const userProfileSchema = z.object({
  id: z.number(),
  email: emailSchema,
  firstName: nameSchema,
  lastName: nameSchema,
  phone: phoneSchema.optional().nullable(),
  avatar: z.string().url().optional().nullable(),
  bio: z.string().optional().nullable(),
  createdAt: z.coerce.date(),
  updatedAt: z.coerce.date(),
});
 
export type UserProfile = z.infer<typeof userProfileSchema>;
 
// ============================================================================
// ADDRESSES
// ============================================================================
 
/**
 * Create address schema
 */
export const createAddressSchema = fullAddressSchema.extend({
  name: nameSchema.optional(),
  phone: phoneSchema.optional(),
});
 
export type CreateAddressInput = z.infer<typeof createAddressSchema>;
 
/**
 * Update address schema (all fields optional)
 */
export const updateAddressSchema = createAddressSchema.partial();
 
export type UpdateAddressInput = z.infer<typeof updateAddressSchema>;
 
// ============================================================================
// PREFERENCES
// ============================================================================
 
/**
 * User notification preferences schema
 */
export const notificationPreferencesSchema = z.object({
  emailMarketing: z.boolean().default(true),
  emailOrders: z.boolean().default(true),
  emailSecurity: z.boolean().default(true),
  pushNotifications: z.boolean().default(false),
  smsNotifications: z.boolean().default(false),
});
 
export type NotificationPreferencesInput = z.infer<typeof notificationPreferencesSchema>;
 
/**
 * User display preferences schema
 */
export const displayPreferencesSchema = z.object({
  theme: z.enum(['light', 'dark', 'system']).default('system'),
  language: z.string().default('en'),
  currency: z.string().default('USD'),
  dateFormat: z.enum(['MM/DD/YYYY', 'DD/MM/YYYY', 'YYYY-MM-DD']).default('MM/DD/YYYY'),
});
 
export type DisplayPreferencesInput = z.infer<typeof displayPreferencesSchema>;