99 lines
2.4 KiB
TypeScript
99 lines
2.4 KiB
TypeScript
|
|
/**
|
||
|
|
* Common Domain - Validation Utilities
|
||
|
|
*
|
||
|
|
* Generic validation functions used across all domains.
|
||
|
|
* These are pure functions with no infrastructure dependencies.
|
||
|
|
*/
|
||
|
|
|
||
|
|
import { z } from "zod";
|
||
|
|
|
||
|
|
/**
|
||
|
|
* UUID validation schema (v4)
|
||
|
|
*/
|
||
|
|
export const uuidSchema = z.string().uuid();
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Normalize and validate an email address
|
||
|
|
*
|
||
|
|
* This is a convenience wrapper that throws on invalid input.
|
||
|
|
* For validation without throwing, use the emailSchema directly with .safeParse()
|
||
|
|
*
|
||
|
|
* @throws Error if email format is invalid
|
||
|
|
*/
|
||
|
|
export function normalizeAndValidateEmail(email: string): string {
|
||
|
|
const emailValidationSchema = z.string().email().transform(e => e.toLowerCase().trim());
|
||
|
|
return emailValidationSchema.parse(email);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Validate a UUID (v4)
|
||
|
|
*
|
||
|
|
* This is a convenience wrapper that throws on invalid input.
|
||
|
|
* For validation without throwing, use the uuidSchema directly with .safeParse()
|
||
|
|
*
|
||
|
|
* @throws Error if UUID format is invalid
|
||
|
|
*/
|
||
|
|
export function validateUuidV4OrThrow(id: string): string {
|
||
|
|
try {
|
||
|
|
return uuidSchema.parse(id);
|
||
|
|
} catch {
|
||
|
|
throw new Error("Invalid user ID format");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Check if a string is a valid email (non-throwing)
|
||
|
|
*/
|
||
|
|
export function isValidEmail(email: string): boolean {
|
||
|
|
return z.string().email().safeParse(email).success;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Check if a string is a valid UUID (non-throwing)
|
||
|
|
*/
|
||
|
|
export function isValidUuid(id: string): boolean {
|
||
|
|
return uuidSchema.safeParse(id).success;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* URL validation schema
|
||
|
|
*/
|
||
|
|
export const urlSchema = z.string().url();
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Validate a URL
|
||
|
|
*
|
||
|
|
* This is a convenience wrapper that throws on invalid input.
|
||
|
|
* For validation without throwing, use the urlSchema directly with .safeParse()
|
||
|
|
*
|
||
|
|
* @throws Error if URL format is invalid
|
||
|
|
*/
|
||
|
|
export function validateUrlOrThrow(url: string): string {
|
||
|
|
try {
|
||
|
|
return urlSchema.parse(url);
|
||
|
|
} catch {
|
||
|
|
throw new Error("Invalid URL format");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Validate a URL (non-throwing)
|
||
|
|
*
|
||
|
|
* Returns validation result with errors if any.
|
||
|
|
* Prefer using urlSchema.safeParse() directly for more control.
|
||
|
|
*/
|
||
|
|
export function validateUrl(url: string): { isValid: boolean; errors: string[] } {
|
||
|
|
const result = urlSchema.safeParse(url);
|
||
|
|
return {
|
||
|
|
isValid: result.success,
|
||
|
|
errors: result.success ? [] : result.error.issues.map(i => i.message),
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Check if a string is a valid URL (non-throwing)
|
||
|
|
*/
|
||
|
|
export function isValidUrl(url: string): boolean {
|
||
|
|
return urlSchema.safeParse(url).success;
|
||
|
|
}
|