Skip to content

Commit

Permalink
docs(arcade): consolidated pathId logic
Browse files Browse the repository at this point in the history
  • Loading branch information
scottrippey committed Dec 23, 2024
1 parent d43fab9 commit d28d6cc
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
5 changes: 5 additions & 0 deletions shared/util/jsonExplorerUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,10 @@ export const isObject = (data: unknown): data is Record<string, unknown> =>
data !== null &&
!Array.isArray(data) &&
!(data instanceof Date);

export const addToPath = (existingPath: string, newSegment: string) =>
existingPath ? `${existingPath}.${newSegment}` : newSegment;

export function getPathId(paths: Array<string | number>) {
return paths.map((v) => String(v)).join(".");
}
10 changes: 4 additions & 6 deletions website/src/arcade/playground/playground-implementation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -98,22 +99,19 @@ 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) {
errorPaths = new Map();
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);
}
}
}
Expand Down

0 comments on commit d28d6cc

Please sign in to comment.