From 995f1243e33ab74ede4b94b6d361a3b6f73e1925 Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Thu, 3 Dec 2026 01:14:08 +0800 Subject: [PATCH] refactor(verification): reuse validated CycloneDX document bytes (#16054) --- scripts/verify-cyclonedx-view.mjs | 46 ++++++++++--------------------- 1 file changed, 14 insertions(+), 32 deletions(-) diff --git a/scripts/verify-cyclonedx-view.mjs b/scripts/verify-cyclonedx-view.mjs index 027675a5d8..12eb2d0ad3 100644 --- a/scripts/verify-cyclonedx-view.mjs +++ b/scripts/verify-cyclonedx-view.mjs @@ -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,