refactor(verification): reuse validated CycloneDX document bytes (#16054)

This commit is contained in:
Vincent Koc
2026-12-03 01:14:08 +08:00
committed by GitHub
parent 22c7535b7e
commit 995f1243e3
+14 -32
View File
@@ -187,12 +187,6 @@ await withVerifierCleanup(async () => {
addFormats(ajv);
for (const file of loaded.slice(0, -1)) ajv.addSchema(file.schema);
const validate = ajv.compile(loaded.at(-1).schema);
const accept = (document) => {
const parsed = JSON.parse(JSON.stringify(document));
if (validate(parsed)) return null;
return ajv.errorsText(validate.errors, { separator: "\n" });
};
const consumerDir = join(root.path, "consumer");
await mkdir(consumerDir);
await writeFile(
@@ -220,9 +214,16 @@ await withVerifierCleanup(async () => {
);
const { Version } = consumerRequire("@cyclonedx/cyclonedx-library/Spec");
const validator = new JsonValidator(Version.v1dot6);
const ingest = async (document) => {
const failure = await validator.validate(JSON.stringify(document));
return failure === null ? null : JSON.stringify(failure);
const assertDocumentAccepted = async (language, bytes) => {
if (!validate(JSON.parse(bytes)))
throw new Error(
`${language} document rejected by CycloneDX schema:\n${ajv.errorsText(validate.errors, { separator: "\n" })}`,
);
const failure = await validator.validate(bytes);
if (failure !== null)
throw new Error(
`${language} document rejected by @cyclonedx/cyclonedx-library:\n${JSON.stringify(failure)}`,
);
};
report.consumer = {
name: "@cyclonedx/cyclonedx-library",
@@ -340,9 +341,8 @@ await withVerifierCleanup(async () => {
),
[localReplacement.path, localReplacement.version],
);
const documentText = JSON.stringify(projected.document);
assert.equal(documentText.includes("pkg:golang/."), false);
assert.equal(documentText.includes("pkg:golang//"), false);
assert.equal(projected.bytes.includes("pkg:golang/."), false);
assert.equal(projected.bytes.includes("pkg:golang//"), false);
assert.equal(
projected.document.components.some((component) => component.hashes),
false,
@@ -353,16 +353,7 @@ await withVerifierCleanup(async () => {
);
if (sum !== undefined) assert.equal(sum.value.startsWith("h1:"), true);
}
const schemaError = accept(projected.document);
if (schemaError !== null)
throw new Error(
`Go document rejected by CycloneDX schema:\n${schemaError}`,
);
const consumerError = await ingest(projected.document);
if (consumerError !== null)
throw new Error(
`Go document rejected by @cyclonedx/cyclonedx-library:\n${consumerError}`,
);
await assertDocumentAccepted("Go", projected.bytes);
report.go = {
compiler: goVersion,
aggregate: projected.document.compositions[0].aggregate,
@@ -477,16 +468,7 @@ await withVerifierCleanup(async () => {
if (pkg.kind === "runtime") assert.equal(scope, "required");
if (pkg.kind === null) assert.equal(scope, undefined);
});
const schemaError = accept(projected.document);
if (schemaError !== null)
throw new Error(
`Rust document rejected by CycloneDX schema:\n${schemaError}`,
);
const consumerError = await ingest(projected.document);
if (consumerError !== null)
throw new Error(
`Rust document rejected by @cyclonedx/cyclonedx-library:\n${consumerError}`,
);
await assertDocumentAccepted("Rust", projected.bytes);
report.rust = {
status: "verified",
rustc,