diff --git a/src/bom/validation.mts b/src/bom/validation.mts new file mode 100644 index 0000000..840f864 --- /dev/null +++ b/src/bom/validation.mts @@ -0,0 +1,34 @@ +import type { SPDX23 } from '../types/bom/spdx-2.3.schema.ts'; +import type { CdxBom, SupportedBom } from '../types/index.mjs'; + +function parseBomOrString(bomOrString: string | object): SupportedBom | null { + if (typeof bomOrString === 'string') { + try { + return JSON.parse(bomOrString); + } catch (e) { + return null; + } + } + return bomOrString as SupportedBom; +} + +export function isCdxBom(bomOrString: string | object): bomOrString is CdxBom { + const bom = parseBomOrString(bomOrString); + return ( + bom !== null && + 'components' in bom && + 'bomFormat' in bom && + bom.bomFormat === 'CycloneDX' + ); +} + +export function isSpdxBom(bomOrString: string | object): bomOrString is SPDX23 { + const bom = parseBomOrString(bomOrString); + return bom !== null && 'SPDXID' in bom && bom.SPDXID === 'SPDXRef-Document'; +} + +export function isSupportedBom( + bomOrString: string | object, +): bomOrString is SupportedBom { + return isCdxBom(bomOrString) || isSpdxBom(bomOrString); +} diff --git a/src/index.mts b/src/index.mts index 802f504..515dd0f 100644 --- a/src/index.mts +++ b/src/index.mts @@ -23,6 +23,9 @@ export type { ExternalReference, Hash, License, + SPDX23, + SupportedBom, } from './types/index.mjs'; export { ComponentScope } from './types/index.mjs'; +export { isCdxBom, isSpdxBom, isSupportedBom } from './bom/validation.mjs'; diff --git a/src/types/eol-scan.mts b/src/types/eol-scan.mts index 3ac274c..04a6b19 100644 --- a/src/types/eol-scan.mts +++ b/src/types/eol-scan.mts @@ -37,7 +37,7 @@ export interface EolReport { } export interface EolReportQueryResponse { - eol: { report: { result: EolReport | null } }; + eol: { report: { report: EolReport | null } }; } export interface EolReportMutationResponse { diff --git a/src/types/index.mts b/src/types/index.mts index 32faf14..69deb94 100644 --- a/src/types/index.mts +++ b/src/types/index.mts @@ -1,4 +1,5 @@ import * as CDX from '@cyclonedx/cyclonedx-library'; +import type { SPDX23 } from './bom/spdx-2.3.schema.ts'; export type CdxBom = CDX.Serialize.JSON.Types.Normalized.Bom; export type Component = CDX.Serialize.JSON.Types.Normalized.Component; @@ -8,4 +9,7 @@ export type License = CDX.Serialize.JSON.Types.Normalized.License; export type ExternalReference = CDX.Serialize.JSON.Types.Normalized.ExternalReference; +export type { SPDX23 }; +export type SupportedBom = CdxBom | SPDX23; + export const ComponentScope = CDX.Enums.ComponentScope;