Files
VibemakerSandbox/app/src/platform/loader/SchemaValidator.js
T
chenlin fc5ee4f2c6 feat: release v0.2 multi-robot simulator
Add declarative robot packages, shared simulation lifecycle and Microban ONNX/PD walking with independent head control.

Unify comic UI and chase camera; default robot packages to a 4.5m arena with real collision walls. Include regression tests, asset provenance/licenses and verification evidence.
2026-10-28 14:00:26 +08:00

117 lines
4.2 KiB
JavaScript

import Ajv from "ajv";
import schema from "./robot.schema.json" with { type: "json" };
import worldSchema from "./world.schema.json" with { type: "json" };
const ajv = new Ajv({ allErrors: true, strict: true }),
check = ajv.compile(schema),
checkWorld = ajv.compile(worldSchema);
function assertSchema(check, value, label) {
if (!check(value))
throw new Error(
`${label}: ${check.errors.map((e) => `${e.instancePath || "/"} ${e.message}`).join("; ")}`,
);
}
export function validateWorld(world) {
assertSchema(checkWorld, world, "world.json");
if (world.boundary && world.type !== "flat")
throw new Error("world.json /boundary: requires a flat World");
if (world.type === "flat" && !world.floor)
throw new Error("world.json /floor: required for flat World");
if (world.presentation && world.type !== "flat")
throw new Error("world.json /presentation: requires a flat World");
if (world.type === "adapter" && !world.adapter)
throw new Error("world.json /adapter: required for adapter World");
return world;
}
export function validatePackage(config) {
assertSchema(check, config, "robot.json");
const groups = config.joints.groups;
const owners = new Set(
config.skills
.filter((s) => s.kind === "primary")
.flatMap((s) => groups[s.owns] ?? []),
),
ids = new Set();
let primaries = 0;
for (const skill of config.skills) {
if (ids.has(skill.id)) throw new Error(`skills: duplicate ${skill.id}`);
ids.add(skill.id);
if (!groups[skill.owns])
throw new Error(`skills.${skill.id}.owns: unknown group ${skill.owns}`);
if (skill.controller.type === "onnx" && !skill.controller.policy)
throw new Error(`skills.${skill.id}.controller.policy: required`);
if (skill.kind === "primary") primaries++;
if (skill.kind === "overlay")
for (const joint of groups[skill.owns]) {
if (owners.has(joint))
throw new Error(`skills: overlapping ownership of ${joint}`);
owners.add(joint);
}
}
if (
config.adapter === "generic" &&
(primaries !== 1 ||
config.skills.some((s) => !["primary", "overlay"].includes(s.kind)))
)
throw new Error(
"generic runtime requires one primary and disjoint overlays",
);
for (const obs of Object.values(config.observations))
for (const term of obs.terms) {
if (term.group && !groups[term.group])
throw new Error(`observations: unknown group ${term.group}`);
if (
["joint_pos_rel", "joint_vel", "last_action"].includes(term.type) &&
!term.group
)
throw new Error(`${term.type}: group required`);
if (term.type === "gyro" && !term.sensor)
throw new Error("gyro: sensor required");
if (term.type === "projected_gravity" && !term.body)
throw new Error("projected_gravity: body required");
if (
term.type === "command" &&
(!term.size || term.size > config.commands.size)
)
throw new Error(
"observations.command: size missing or exceeds command buffer",
);
}
for (const action of config.actions)
if (!groups[action.group])
throw new Error(`actions: unknown group ${action.group}`);
if (
config.actuation.type === "joint_pd" &&
!["kp", "kd", "torque_limit"].every((k) =>
Number.isFinite(config.actuation[k]),
)
)
throw new Error("actuation: PD gains and torque_limit required");
const codes = new Set();
for (const binding of config.input.bindings) {
if (
config.adapter === "generic" &&
![
"move_forward",
"move_backward",
"turn_left",
"turn_right",
"reset",
"pause",
"chaseToggle",
...config.skills
.filter((s) => s.kind === "overlay")
.map((s) => `toggle:${s.id}`),
].includes(binding.action)
)
throw new Error(`input: unsupported action ${binding.action}`);
for (const code of binding.codes) {
if (codes.has(code)) throw new Error(`input: duplicate ${code}`);
codes.add(code);
}
}
if (config.adapter === "generic" && config.render.source !== "mjcf_visual")
throw new Error("render.source: generic runtime requires mjcf_visual");
return config;
}
export { schema as robotSchema };