All files / src/packlets/project-format parser.ts

100% Statements 9/9
100% Branches 8/8
100% Functions 2/2
100% Lines 8/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24                        14x 14x 1x   12x 12x 8x 8x   4x    
/**
 * Parser for Beat Muser project files.
 *
 * Parses a JSON string into a validated `ProjectFile` object.
 * Throws a descriptive error if the data does not conform to the schema.
 */
 
import { Errors } from "typebox/value";
import { ProjectFileSchema } from "./schema";
import type { ProjectFile } from "./types";
 
export function parseProjectFile(json: string): ProjectFile {
  const parsed = JSON.parse(json);
  if (!parsed || typeof parsed !== "object") {
    throw new Error("Invalid project file: expected a JSON object");
  }
  const errors = Errors(ProjectFileSchema, parsed);
  if (errors.length > 0) {
    const details = errors.map((e) => `${e.instancePath || "/"}: ${e.message}`).join(", ");
    throw new Error(`Invalid project file: ${details}`);
  }
  return parsed;
}