Skip to content

Check a file from a Node script ​

Your own tool writes or edits content files, and you want it to check them the way alta lint does, without running a command.

Solution ​

The SDK is not on npm yet: build it in your copy of the Alta OS repository, then add it to your project.

Terminal
sh
npm run build --prefix ~/alta-os/packages/sdk
npm install ~/alta-os/packages/sdk

Save this script as validate-file.mjs:

validate-file.mjs
js
// Checks one content file from a Node script and prints each problem with its rule and the page that explains it.
// Usage: node validate-file.mjs my-pack/quests.json
import { readFileSync } from "node:fs";
import { ruleInfo, ruleUrl, validate } from "@altasystem/sdk";

const file = process.argv[2] ?? "my-pack/quests.json";
const result = validate(JSON.parse(readFileSync(file, "utf8")));

for (const p of result.problems) {
  console.log(`${file} › ${p.field ?? "(file)"}: ${p.message} [${p.rule}]`);
  console.log(`  fix: ${ruleInfo(p.rule)?.fix}`);
  console.log(`  docs: ${ruleUrl(p.rule)}`);
}
console.log(result.ok ? `ok ${file} (${result.kind})` : `${result.problems.length} problem(s) in ${file}`);
process.exitCode = result.ok ? 0 : 1;

examples/recipes/sdk/validate-file.mjs

Run it on a quest file whose step names an input it does not have:

Terminal
sh
node validate-file.mjs broken.json
Output
text
broken.json › quests[0].steps[2].run.args.name: core.my-pack.welcome/name: run.args.name: unknown input nmae [step.input-ref]
  fix: Write `{ "input": "<id of an input of this step>" }` or `{ "answer": "answers.<key>" }`, and feed every input to an argument (or `saves` it).
  docs: https://docs.os.altasystem.ai/reference/errors#step-input-ref
1 problem(s) in broken.json

How it works ​

validate finds the standard from the file's schema field and returns ok, the problems and the warnings. Each problem carries the field, the message and the rule id; ruleInfo gives the rule's summary and fix, and ruleUrl its section of the errors page. The SDK is pure: it reads nothing from disk and sends nothing anywhere.

Variants ​

  • Against the game. Without a game, a quest file is checked on its own (a warning says so). Pass { game } as the second argument to also check its links to the game's quests.
  • The context lines. A problem of a step that does something also has context (where, detail, hint): the in, detail and hint lines of alta lint.
  • A whole folder. @altasystem/sdk/node reads a folder; or run alta lint <folder> --json and read its report.

What's next

Alta OS is open source (MIT).