mirror of
https://github.com/chartdb/chartdb.git
synced 2025-11-07 23:43:20 +00:00
fix: diff logic (#927)
This commit is contained in:
70
src/lib/domain/diff/area-diff.ts
Normal file
70
src/lib/domain/diff/area-diff.ts
Normal 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>>;
|
||||
};
|
||||
Reference in New Issue
Block a user