Первая версия
This commit is contained in:
38
src/core/ChangeSetValidator.js
Normal file
38
src/core/ChangeSetValidator.js
Normal file
@@ -0,0 +1,38 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user