All files / src/lib/validation schemas.ts

100% Statements 252/252
100% Branches 4/4
100% Functions 1/1
100% Lines 252/252

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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 2531x 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 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 5x 2x 2x 3x 3x 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  
/**
 * Centralized Validation Schemas
 *
 * Common reusable Zod schemas for API input validation.
 * Import these schemas in API routes to ensure consistent validation.
 */
 
import { z } from 'zod';
 
// ============================================================================
// PRIMITIVE SCHEMAS - Reusable building blocks
// ============================================================================
 
/**
 * ID schema for database IDs (positive integers)
 */
export const idSchema = z.coerce.number().int().positive('ID must be a positive integer');
 
/**
 * Optional ID schema (for nullable foreign keys)
 */
export const optionalIdSchema = idSchema.nullable().optional();
 
/**
 * UUID schema for string UUIDs
 */
export const uuidSchema = z.string().uuid('Invalid UUID format');
 
/**
 * Email schema with standard validation
 */
export const emailSchema = z
  .string()
  .min(1, 'Email is required')
  .trim()
  .toLowerCase()
  .email('Invalid email address')
  .max(255, 'Email must be less than 255 characters');
 
/**
 * Password schema with minimum security requirements
 */
export const passwordSchema = z
  .string()
  .min(8, 'Password must be at least 8 characters')
  .max(128, 'Password must be less than 128 characters')
  .regex(
    /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)/,
    'Password must contain at least one lowercase letter, one uppercase letter, and one number'
  );
 
/**
 * Simple password schema (for login - no complexity requirements)
 */
export const simplePasswordSchema = z
  .string()
  .min(1, 'Password is required')
  .max(128, 'Password must be less than 128 characters');
 
/**
 * Name schema for user names
 */
export const nameSchema = z
  .string()
  .min(2, 'Name must be at least 2 characters')
  .max(100, 'Name must be less than 100 characters')
  .trim();
 
/**
 * Phone number schema
 */
export const phoneSchema = z
  .string()
  .min(10, 'Phone number must be at least 10 digits')
  .max(20, 'Phone number must be less than 20 characters')
  .regex(/^[+]?[\d\s()-]+$/, 'Invalid phone number format');
 
/**
 * URL schema
 */
export const urlSchema = z.string().url('Invalid URL format').max(2048, 'URL is too long');
 
/**
 * Slug schema for URL-friendly strings
 */
export const slugSchema = z
  .string()
  .min(1, 'Slug is required')
  .max(200, 'Slug must be less than 200 characters')
  .regex(
    /^[a-z0-9]+(?:-[a-z0-9]+)*$/,
    'Slug must be URL-friendly (lowercase letters, numbers, and hyphens only)'
  );
 
// ============================================================================
// PAGINATION SCHEMAS
// ============================================================================
 
/**
 * Standard pagination schema for list endpoints
 */
export const paginationSchema = z.object({
  page: z.coerce.number().int().positive().optional().default(1),
  limit: z.coerce.number().int().min(1).max(100).optional().default(20),
  sortBy: z.string().optional(),
  sortOrder: z.enum(['asc', 'desc']).optional().default('desc')});
 
/**
 * Extended pagination with search
 */
export const searchPaginationSchema = paginationSchema.extend({
  search: z.string().max(200, 'Search query too long').optional(),
  q: z.string().max(200, 'Search query too long').optional()});
 
// ============================================================================
// ADDRESS SCHEMAS
// ============================================================================
 
/**
 * Address type enum
 */
export const addressTypeSchema = z.enum(['SHIPPING', 'BILLING']);
 
/**
 * Base address schema
 */
export const addressSchema = z.object({
  street: z.string().min(5, 'Street must be at least 5 characters').max(255),
  city: z.string().min(2, 'City must be at least 2 characters').max(100),
  state: z.string().min(2, 'State must be at least 2 characters').max(100),
  zipCode: z.string().min(3, 'Zip code must be at least 3 characters').max(20),
  country: z.string().min(2, 'Country must be at least 2 characters').max(100)});
 
/**
 * Full address schema with type
 */
export const fullAddressSchema = addressSchema.extend({
  type: addressTypeSchema,
  isDefault: z.boolean().optional().default(false)});
 
// ============================================================================
// MONETARY SCHEMAS
// ============================================================================
 
/**
 * Price schema (positive decimal)
 */
export const priceSchema = z.coerce.number().positive('Price must be positive');
 
/**
 * Non-negative number (for stock, quantities)
 */
export const nonNegativeSchema = z.coerce.number().nonnegative('Value cannot be negative');
 
/**
 * Quantity schema (positive integer)
 */
export const quantitySchema = z.coerce.number().int().positive('Quantity must be at least 1');
 
// ============================================================================
// DATE SCHEMAS
// ============================================================================
 
/**
 * Date string schema (ISO format)
 */
export const dateStringSchema = z.string().datetime({ message: 'Invalid date format' });
 
/**
 * Coerced date schema
 */
export const dateSchema = z.coerce.date();
 
/**
 * Date range schema
 */
export const dateRangeSchema = z
  .object({
    dateFrom: dateSchema.optional(),
    dateTo: dateSchema.optional()})
  .refine(
    (data) => {
      if (data.dateFrom && data.dateTo) {
        return data.dateFrom <= data.dateTo;
      }
      return true;
    },
    { message: 'Start date must be before end date' }
  );
 
// ============================================================================
// CONTENT SCHEMAS
// ============================================================================
 
/**
 * Short text schema (titles, names)
 */
export const shortTextSchema = z
  .string()
  .min(1, 'This field is required')
  .max(255, 'Text must be less than 255 characters')
  .trim();
 
/**
 * Long text schema (descriptions, content)
 */
export const longTextSchema = z
  .string()
  .min(1, 'This field is required')
  .max(10000, 'Text must be less than 10000 characters')
  .trim();
 
/**
 * Optional text schema
 */
export const optionalTextSchema = z.string().max(10000).optional();
 
// ============================================================================
// FILE SCHEMAS
// ============================================================================
 
/**
 * File metadata schema
 */
export const fileMetadataSchema = z.object({
  fileName: z.string().min(1).max(255),
  fileType: z.string().min(1).max(100),
  fileSize: z
    .number()
    .int()
    .positive()
    .max(10 * 1024 * 1024, 'File size must be less than 10MB')});
 
/**
 * Image URL schema
 */
export const imageUrlSchema = z.object({
  url: urlSchema,
  thumbnailUrl: urlSchema.optional(),
  alt: z.string().max(255).optional()});
 
// ============================================================================
// TYPE EXPORTS
// ============================================================================
 
export type PaginationInput = z.infer<typeof paginationSchema>;
export type SearchPaginationInput = z.infer<typeof searchPaginationSchema>;
export type AddressInput = z.infer<typeof addressSchema>;
export type FullAddressInput = z.infer<typeof fullAddressSchema>;
export type DateRangeInput = z.infer<typeof dateRangeSchema>;
export type FileMetadataInput = z.infer<typeof fileMetadataSchema>;
export type ImageUrlInput = z.infer<typeof imageUrlSchema>;