import { z } from 'zod'; import { pageSchema } from '../identity/identity.schemas'; const timestamp = z.iso .datetime({ offset: true }) .refine( (value) => (value.match(/\.(\d+)/)?.[1].length ?? 0) <= 3, 'Use at most millisecond precision', ) .transform((value) => new Date(value)) .refine( (value) => value.getUTCFullYear() >= 1 && value.getUTCFullYear() <= 9999, 'Use UTC years 0001 through 9999', ); const fields = { from: timestamp, to: timestamp, currency: z .string() .regex(/^[A-Z]{3}$/) .optional(), }; const bounded = (input: { from: Date; to: Date }) => input.to > input.from && input.to.getTime() - input.from.getTime() <= 366 * 86400000; export const analyticsWindowSchema = z .object(fields) .strict() .refine(bounded, { message: 'Use an increasing window of at most 366 days', path: ['to'], }); export const analyticsVariantsSchema = pageSchema .extend(fields) .strict() .refine(bounded, { message: 'Use an increasing window of at most 366 days', path: ['to'], }); export type AnalyticsWindow = z.infer; export type AnalyticsVariants = z.infer;