fix: diff logic (#927)

This commit is contained in:
Guy Ben-Aharon
2025-09-20 19:40:04 +03:00
committed by GitHub
parent 93d72a896b
commit 1b8d51b73c
5 changed files with 1629 additions and 215 deletions

View File

@@ -0,0 +1,70 @@
import { z } from 'zod';
import type { Area } from '../area';
export type AreaDiffAttribute = keyof Pick<Area, 'name' | 'color'>;
const areaDiffAttributeSchema: z.ZodType<AreaDiffAttribute> = z.union([
z.literal('name'),
z.literal('color'),
]);
export interface AreaDiffChanged {
object: 'area';
type: 'changed';
areaId: string;
attribute: AreaDiffAttribute;
oldValue?: string | null;
newValue?: string | null;
}
export const AreaDiffChangedSchema: z.ZodType<AreaDiffChanged> = z.object({
object: z.literal('area'),
type: z.literal('changed'),
areaId: z.string(),
attribute: areaDiffAttributeSchema,
oldValue: z.string().or(z.null()).optional(),
newValue: z.string().or(z.null()).optional(),
});
export interface AreaDiffRemoved {
object: 'area';
type: 'removed';
areaId: string;
}
export const AreaDiffRemovedSchema: z.ZodType<AreaDiffRemoved> = z.object({
object: z.literal('area'),
type: z.literal('removed'),
areaId: z.string(),
});
export interface AreaDiffAdded<T = Area> {
object: 'area';
type: 'added';
areaAdded: T;
}
export const createAreaDiffAddedSchema = <T = Area>(
areaSchema: z.ZodType<T>
): z.ZodType<AreaDiffAdded<T>> => {
return z.object({
object: z.literal('area'),
type: z.literal('added'),
areaAdded: areaSchema,
}) as z.ZodType<AreaDiffAdded<T>>;
};
export type AreaDiff<T = Area> =
| AreaDiffChanged
| AreaDiffRemoved
| AreaDiffAdded<T>;
export const createAreaDiffSchema = <T = Area>(
areaSchema: z.ZodType<T>
): z.ZodType<AreaDiff<T>> => {
return z.union([
AreaDiffChangedSchema,
AreaDiffRemovedSchema,
createAreaDiffAddedSchema(areaSchema),
]) as z.ZodType<AreaDiff<T>>;
};