48 lines
1.4 KiB
TypeScript

/**
* Domain Toolkit - Validation Helpers
*
* Common validation utilities that can be reused across all domains.
*/
import { z } from "zod";
// ============================================================================
// Zod Schema Helpers
// ============================================================================
/**
* Create a schema for pagination parameters
*/
export function createPaginationSchema(options?: {
minLimit?: number;
maxLimit?: number;
defaultLimit?: number;
}) {
const { minLimit = 1, maxLimit = 100, defaultLimit = 10 } = options ?? {};
return z.object({
page: z.coerce.number().int().positive().optional().default(1),
limit: z.coerce.number().int().min(minLimit).max(maxLimit).optional().default(defaultLimit),
offset: z.coerce.number().int().nonnegative().optional(),
});
}
// ============================================================================
// Type Guards
// ============================================================================
/**
* Type guard for checking if value is a record
*/
export function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
/**
* Type guard for checking if error is a Zod error
*/
export function isZodError(error: unknown): error is z.ZodError {
if (!isRecord(error)) return false;
return Array.isArray(error["issues"]);
}