import { z } from 'zod'; import type { Note } from '../note'; export type NoteDiffAttribute = keyof Pick< Note, 'content' | 'color' | 'x' | 'y' | 'width' | 'height' >; const noteDiffAttributeSchema: z.ZodType = z.union([ z.literal('content'), z.literal('color'), z.literal('x'), z.literal('y'), z.literal('width'), z.literal('height'), ]); export interface NoteDiffChanged { object: 'note'; type: 'changed'; noteId: string; attribute: NoteDiffAttribute; oldValue?: string | number | null; newValue?: string | number | null; } export const NoteDiffChangedSchema: z.ZodType = z.object({ object: z.literal('note'), type: z.literal('changed'), noteId: z.string(), attribute: noteDiffAttributeSchema, oldValue: z.union([z.string(), z.number(), z.null()]).optional(), newValue: z.union([z.string(), z.number(), z.null()]).optional(), }); export interface NoteDiffRemoved { object: 'note'; type: 'removed'; noteId: string; } export const NoteDiffRemovedSchema: z.ZodType = z.object({ object: z.literal('note'), type: z.literal('removed'), noteId: z.string(), }); export interface NoteDiffAdded { object: 'note'; type: 'added'; noteAdded: T; } export const createNoteDiffAddedSchema = ( noteSchema: z.ZodType ): z.ZodType> => { return z.object({ object: z.literal('note'), type: z.literal('added'), noteAdded: noteSchema, }) as z.ZodType>; }; export type NoteDiff = | NoteDiffChanged | NoteDiffRemoved | NoteDiffAdded; export const createNoteDiffSchema = ( noteSchema: z.ZodType ): z.ZodType> => { return z.union([ NoteDiffChangedSchema, NoteDiffRemovedSchema, createNoteDiffAddedSchema(noteSchema), ]) as z.ZodType>; };