all repos — sz-chrome-remote-cdp @ master

pi-browser-harness, patched to drive a Chrome on another host over CDP, with castle_cdp_search web search

src/schemas/parse.ts (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
import type { Static, TSchema } from "typebox";
import type { Validator } from "typebox/compile";
import { type Result, err, ok } from "../util/result";

export const parseJson = <S extends TSchema>(
  raw: string,
  validator: Validator<{}, S>,
): Result<Static<S>, string> => {
  let parsed: unknown;
  try {
    parsed = JSON.parse(raw);
  } catch (e) {
    return err(`JSON parse failed: ${e instanceof Error ? e.message : String(e)}`);
  }
  if (!validator.Check(parsed)) {
    const messages = validator
      .Errors(parsed)
      .map((x) => x.message)
      .join("; ");
    return err(messages.length > 0 ? messages : "JSON did not match the expected shape");
  }
  return ok(parsed);
};