|
| 1 | +/*! |
| 2 | +This file is part of CycloneDX JavaScript Library. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +
|
| 16 | +SPDX-License-Identifier: Apache-2.0 |
| 17 | +Copyright (c) OWASP Foundation. All Rights Reserved. |
| 18 | +*/ |
| 19 | + |
| 20 | +import CDX from "@cyclonedx/cyclonedx-library" |
| 21 | +import {readFile} from 'node:fs/promises'; |
| 22 | + |
| 23 | +const args = process.argv.slice(2); |
| 24 | +if (args.length !== 1) { |
| 25 | + console.error("missing args"); |
| 26 | + process.exit(1); |
| 27 | +} |
| 28 | +const [filePath] = args |
| 29 | +console.debug('filePath', filePath) |
| 30 | + |
| 31 | +const json = await readFile(filePath, 'utf8') |
| 32 | +const data = JSON.parse(json); |
| 33 | + |
| 34 | +const CDX_JSON_SCHEMA_RE = /^http:\/\/cyclonedx\.org\/schema\/bom\-(\d+\.\d+)\.schema\.json$/ |
| 35 | +const specVersion = data['$schema'].match(CDX_JSON_SCHEMA_RE)[1] |
| 36 | +const validator = new CDX.Validation.JsonStrictValidator(specVersion) |
| 37 | + |
| 38 | +const validationError = await validator.validate(json) |
| 39 | +if (validationError !== null) { |
| 40 | + console.error('validation error', validationError) |
| 41 | + process.exit(2); |
| 42 | +} |
| 43 | + |
| 44 | +console.info('valid') |
| 45 | +process.exit(0) |
0 commit comments