From d28d6cc94daad29c1596aff205f3a86b99990573 Mon Sep 17 00:00:00 2001 From: scottrippey Date: Mon, 23 Dec 2024 09:57:44 -0700 Subject: [PATCH] docs(arcade): consolidated pathId logic --- shared/util/jsonExplorerUtils.ts | 5 +++++ .../src/arcade/playground/playground-implementation.ts | 10 ++++------ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/shared/util/jsonExplorerUtils.ts b/shared/util/jsonExplorerUtils.ts index 80efa826..3ce485d0 100644 --- a/shared/util/jsonExplorerUtils.ts +++ b/shared/util/jsonExplorerUtils.ts @@ -8,5 +8,10 @@ export const isObject = (data: unknown): data is Record => data !== null && !Array.isArray(data) && !(data instanceof Date); + export const addToPath = (existingPath: string, newSegment: string) => existingPath ? `${existingPath}.${newSegment}` : newSegment; + +export function getPathId(paths: Array) { + return paths.map((v) => String(v)).join("."); +} diff --git a/website/src/arcade/playground/playground-implementation.ts b/website/src/arcade/playground/playground-implementation.ts index 0a6a3763..5bd3673a 100644 --- a/website/src/arcade/playground/playground-implementation.ts +++ b/website/src/arcade/playground/playground-implementation.ts @@ -8,6 +8,7 @@ import toast from "react-hot-toast"; import * as q from "groqd"; import { GroqBuilder, ValidationErrors } from "groq-builder"; import type * as PlaygroundModule from "./index"; +import { getPathId } from "../../../../shared/util/jsonExplorerUtils"; /** * This returns @@ -98,7 +99,7 @@ const runQueryInternal = async ({ if (err instanceof ValidationErrors) { errorPaths = new Map(); for (const e of err.errors) { - const pathId = e.path.map((v) => String(v)).join("."); + const pathId = getPathId(e.path); errorPaths.set(pathId, e.message); } } else if (err instanceof q.GroqdParseError) { @@ -106,14 +107,11 @@ const runQueryInternal = async ({ for (const e of err.zodError.errors) { if (e.message === "Required" && !has(err.rawResponse, e.path)) { errorPaths.set( - e.path - .slice(0, -1) - .map((v) => String(v)) - .join("."), + getPathId(e.path.slice(0, -1)), `Field "${e.path.at(-1)}" is Required` ); } else { - errorPaths.set(e.path.map((v) => String(v)).join("."), e.message); + errorPaths.set(getPathId(e.path), e.message); } } }