From 03f12f6aa42776dde070969940b187c53ac4f7fb Mon Sep 17 00:00:00 2001 From: Alex Yang Date: Tue, 1 Aug 2023 12:13:24 -0700 Subject: [PATCH] feat: add filter schema (#3479) --- packages/env/package.json | 1 + packages/env/src/automation.ts | 12 ++++++ packages/env/src/filter.ts | 73 ++++++++++++++++++++++------------ 3 files changed, 61 insertions(+), 25 deletions(-) create mode 100644 packages/env/src/automation.ts diff --git a/packages/env/package.json b/packages/env/package.json index 6414c9b360..52c50c4ecc 100644 --- a/packages/env/package.json +++ b/packages/env/package.json @@ -12,6 +12,7 @@ "zod": "^3.21.4" }, "exports": { + "./automation": "./src/automation.ts", "./global": "./src/global.ts", "./constant": "./src/constant.ts", "./workspace": "./src/workspace.ts", diff --git a/packages/env/src/automation.ts b/packages/env/src/automation.ts new file mode 100644 index 0000000000..08b53a8dd0 --- /dev/null +++ b/packages/env/src/automation.ts @@ -0,0 +1,12 @@ +import type { z } from 'zod'; + +export type Action< + InputSchema extends z.ZodObject, + Args extends readonly any[], +> = { + id: string; + name: string; + description: string; + inputSchema: InputSchema; + action: (input: z.input, ...args: Args) => void; +}; diff --git a/packages/env/src/filter.ts b/packages/env/src/filter.ts index 0f45e6c1c6..4b41f9a389 100644 --- a/packages/env/src/filter.ts +++ b/packages/env/src/filter.ts @@ -1,4 +1,14 @@ import type { Workspace } from '@blocksuite/store'; +import { z } from 'zod'; + +export const literalValueSchema: z.ZodType = + z.union([ + z.number(), + z.string(), + z.boolean(), + z.array(z.lazy(() => literalValueSchema)), + z.record(z.lazy(() => literalValueSchema)), + ]); export type LiteralValue = | number @@ -7,6 +17,11 @@ export type LiteralValue = | { [K: string]: LiteralValue } | Array; +export const refSchema: z.ZodType = z.object({ + type: z.literal('ref'), + name: z.never(), +}); + export type Ref = { type: 'ref'; name: keyof VariableMap; @@ -15,32 +30,40 @@ export type Ref = { // eslint-disable-next-line @typescript-eslint/no-empty-interface export interface VariableMap {} -export type Literal = { - type: 'literal'; - value: LiteralValue; -}; +export const literalSchema = z.object({ + type: z.literal('literal'), + value: literalValueSchema, +}); -export type Filter = { - type: 'filter'; - left: Ref; - funcName: string; - args: Literal[]; -}; +export type Literal = z.input; -export type Collection = { - id: string; - workspaceId: string; - name: string; - pinned?: boolean; - filterList: Filter[]; - allowList?: string[]; - excludeList?: string[]; -}; +export const filterSchema = z.object({ + type: z.literal('filter'), + left: refSchema, + funcName: z.string(), + args: z.array(literalSchema), +}); + +export type Filter = z.input; + +export const collectionSchema = z.object({ + id: z.string(), + workspaceId: z.string(), + name: z.string(), + pinned: z.boolean().optional(), + filterList: z.array(filterSchema), + allowList: z.array(z.string()).optional(), + excludeList: z.array(z.string()).optional(), +}); + +export type Collection = z.input; + +export const tagSchema = z.object({ + id: z.string(), + value: z.string(), + color: z.string(), + parentId: z.string().optional(), +}); +export type Tag = z.input; -export type Tag = { - id: string; - value: string; - color: string; - parentId?: string; -}; export type PropertiesMeta = Workspace['meta']['properties'];