|
| 1 | +import Ajv from 'ajv/dist/jtd.js' |
| 2 | + |
| 3 | +import license_information from '../../lib/license/license_information.js' |
| 4 | +import { validate } from 'license-expressions' |
| 5 | + |
| 6 | +const ajv = new Ajv() |
| 7 | + |
| 8 | +const CONSIDERED_LICENSE_KEYS = new Set( |
| 9 | + license_information.licenses |
| 10 | + .filter((license) => !license.deprecated) |
| 11 | + .map((license) => license.license_key) |
| 12 | +) |
| 13 | + |
| 14 | +const inputSchema = /** @type {const} */ ({ |
| 15 | + additionalProperties: true, |
| 16 | + properties: { |
| 17 | + document: { |
| 18 | + additionalProperties: true, |
| 19 | + properties: { |
| 20 | + license_expression: { |
| 21 | + type: 'string', |
| 22 | + }, |
| 23 | + }, |
| 24 | + }, |
| 25 | + }, |
| 26 | +}) |
| 27 | + |
| 28 | +const validateInput = ajv.compile(inputSchema) |
| 29 | + |
| 30 | +/** |
| 31 | + * @param {string} licenseToCheck |
| 32 | + */ |
| 33 | +export function checkLicense(licenseToCheck) { |
| 34 | + // first do asimple check with aboutcode and spdx license ids |
| 35 | + // then check whether the license is a valid SPDX license expression |
| 36 | + return ( |
| 37 | + CONSIDERED_LICENSE_KEYS.has(licenseToCheck) || |
| 38 | + validate(licenseToCheck).valid |
| 39 | + ) |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * It MUST be tested that the all license identifiers and exceptions are listed either |
| 44 | + * in the official SPDX license identifier list or AboutCode's "ScanCode LicenseDB". |
| 45 | + * @param {unknown} doc |
| 46 | + * @returns |
| 47 | + */ |
| 48 | +export function informativeTest_6_3_17(doc) { |
| 49 | + const ctx = { |
| 50 | + infos: /** @type {Array<{ message: string; instancePath: string }>} */ ([]), |
| 51 | + } |
| 52 | + |
| 53 | + if (!validateInput(doc)) { |
| 54 | + return ctx |
| 55 | + } |
| 56 | + |
| 57 | + const licenseToCheck = doc.document.license_expression |
| 58 | + |
| 59 | + if (!checkLicense(licenseToCheck)) { |
| 60 | + ctx.infos.push({ |
| 61 | + instancePath: '/document/license_expression', |
| 62 | + message: `Invalid license: '${licenseToCheck}'`, |
| 63 | + }) |
| 64 | + } |
| 65 | + |
| 66 | + return ctx |
| 67 | +} |
0 commit comments