use range instead of dates across the web

This commit is contained in:
Carl-Gerhard Lindesvärd
2023-10-28 21:58:17 +02:00
parent c8c86d8c23
commit c5823dc4cb
11 changed files with 156 additions and 96 deletions

View File

@@ -1,9 +1,9 @@
import { z } from "zod";
import { operators, chartTypes, intervals } from "./constants";
function objectToZodEnums<K extends string> ( obj: Record<K, any> ): [ K, ...K[] ] {
const [ firstKey, ...otherKeys ] = Object.keys( obj ) as K[]
return [ firstKey!, ...otherKeys ]
function objectToZodEnums<K extends string>(obj: Record<K, any>): [K, ...K[]] {
const [firstKey, ...otherKeys] = Object.keys(obj) as K[];
return [firstKey!, ...otherKeys];
}
export const zChartEvent = z.object({
@@ -15,13 +15,7 @@ export const zChartEvent = z.object({
id: z.string(),
name: z.string(),
operator: z.enum(objectToZodEnums(operators)),
value: z.array(
z
.string()
.or(z.number())
.or(z.boolean())
.or(z.null())
),
value: z.array(z.string().or(z.number()).or(z.boolean()).or(z.null())),
}),
),
});
@@ -39,10 +33,22 @@ export const zTimeInterval = z.enum(objectToZodEnums(intervals));
export const zChartInput = z.object({
name: z.string(),
startDate: z.string(),
endDate: z.string(),
chartType: zChartType,
interval: zTimeInterval,
events: zChartEvents,
breakdowns: zChartBreakdowns,
range: z
.literal(0)
.or(z.literal(1))
.or(z.literal(7))
.or(z.literal(14))
.or(z.literal(30))
.or(z.literal(90))
.or(z.literal(180))
.or(z.literal(365)),
});
export const zChartInputWithDates = zChartInput.extend({
startDate: z.string().nullish(),
endDate: z.string().nullable(),
});