Files
web_app/src/core/ChangeSetValidator.js
2026-02-23 09:08:15 +03:00

39 lines
1.3 KiB
JavaScript

import { PathUtils } from "./PathUtils.js";
export class ChangeSetValidator {
validate(changeset) {
if (!Array.isArray(changeset)) throw new Error("changeset must be array");
const normalized = [];
for (const item of changeset) {
if (!item || typeof item !== "object") throw new Error("invalid changeset item");
if (!["create", "update", "delete"].includes(item.op)) throw new Error("invalid op");
const path = PathUtils.normalizeRelative(item.path);
if (item.op === "create") {
if (typeof item.proposed_content !== "string") throw new Error("create needs proposed_content");
}
if (item.op === "update") {
if (typeof item.base_hash !== "string") throw new Error("update needs base_hash");
if (typeof item.proposed_content !== "string") throw new Error("update needs proposed_content");
}
if (item.op === "delete") {
if (typeof item.base_hash !== "string") throw new Error("delete needs base_hash");
if ("proposed_content" in item) throw new Error("delete forbids proposed_content");
}
normalized.push({
op: item.op,
path,
base_hash: item.base_hash || null,
proposed_content: item.proposed_content ?? null,
reason: item.reason || ""
});
}
return normalized;
}
}