File size: 1,084 Bytes
4e23b01
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
 * Shared schema helpers for contract fragments.
 */

import { z } from 'zod';

/** `Page<T>` on the wire (`persistence/interface/queryStore.ts`). */
export const pageOf = <T extends z.ZodType>(item: T) =>
  z.object({
    items: z.array(item),
    nextCursor: z.string().optional(),
  });

/**
 * Engine `X | undefined` returns cross the wire as `null` over HTTP (JSON
 * has no `undefined`) and as `undefined` in-process — accept both and
 * normalize to `undefined`.
 */
export const maybe = <T extends z.ZodType>(schema: T) =>
  z.union([schema, z.null(), z.undefined()]).transform((value) => value ?? undefined);

/**
 * `void` method results arrive as `null` over HTTP (JSON has no `undefined`)
 * and as `undefined` in-process — accept both and normalize to `undefined`.
 */
export const noResult = z
  .union([z.void(), z.null()])
  .transform(() => undefined);

/** Engine `{ added, removed, changed }` change-set events. */
export const stringDeltaSchema = z.object({
  added: z.array(z.string()),
  removed: z.array(z.string()),
  changed: z.array(z.string()),
});