/** Read-only source diagnostics. These do not certify a dataset or retain source evidence. */ import { z } from 'zod'; import { parseInstrumentId } from '@stock-bot/types'; const count = z.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER); const instant = z.number().int().nonnegative().max(8_640_000_000_000_000); export const storedSourceIntervalSchema = z.enum(['1m', '5m', '1h']); const instrument = z.string().refine(value => { try { return parseInstrumentId(value).id === value; } catch { return false; } }, 'Expected a provider instrument identity, for example AAPL.US'); export const storedSourceSelectionSchema = z .object({ instrumentId: instrument, interval: storedSourceIntervalSchema }) .strict(); export type StoredSourceSelection = z.infer; export const storedSourceQuerySchema = storedSourceSelectionSchema .extend({ start: instant, end: instant, }) .strict() .refine(value => value.start < value.end, 'End must be after start'); export type StoredSourceQuery = z.infer; export const storedSourceCoverageSchema = z .object({ version: z.literal('stored-eod-coverage/v1'), observedAt: instant, query: storedSourceSelectionSchema, series: z .array( z .object({ instrumentId: instrument, interval: storedSourceIntervalSchema, rowCount: count, firstTimestamp: instant.nullable(), lastTimestamp: instant.nullable(), invalidTimestampRows: count, }) .strict() ) .max(1), }) .strict(); export type StoredSourceCoverage = z.infer; export const storedSourceIssueSchema = z.enum([ 'identity-mismatch', 'invalid-timestamp', 'outside-range', 'invalid-prices', 'null-volume', 'invalid-volume', 'duplicate-timestamp', ]); export const storedSourceInspectionSchema = z .object({ version: z.literal('stored-eod-inspection/v1'), observedAt: instant, query: storedSourceQuerySchema, rowCount: count, invalidRows: count, nullVolumeRows: count, duplicateRows: count, firstTimestamp: instant.nullable(), lastTimestamp: instant.nullable(), days: z.array( z .object({ date: z.string().regex(/^\d{4}-\d{2}-\d{2}$/), rowCount: count, invalidRows: count, }) .strict() ), issues: z.array( z .object({ code: storedSourceIssueSchema, rowCount: count, timestamps: z.array(instant.nullable()), }) .strict() ), }) .strict(); export type StoredSourceInspection = z.infer;