|
| 1 | +import Ajv from 'ajv/dist/jtd.js' |
| 2 | +import { |
| 3 | + existsReferenceWithSummaryAndCategory, |
| 4 | + getTranslationInDocumentLang, |
| 5 | + isLangSpecifiedAndNotEnglish, |
| 6 | +} from '../../lib/shared/languageSpecificTranslation.js' |
| 7 | + |
| 8 | +const ajv = new Ajv() |
| 9 | + |
| 10 | +/* |
| 11 | + This is the jtd schema that needs to match the input document so that the |
| 12 | + test is activated. If this schema doesn't match it normally means that the input |
| 13 | + document does not validate against the csaf json schema or optional fields that |
| 14 | + the test checks are not present. |
| 15 | + */ |
| 16 | +const inputSchema = /** @type {const} */ ({ |
| 17 | + additionalProperties: true, |
| 18 | + properties: { |
| 19 | + document: { |
| 20 | + additionalProperties: true, |
| 21 | + properties: { |
| 22 | + category: { type: 'string' }, |
| 23 | + }, |
| 24 | + optionalProperties: { |
| 25 | + lang: { |
| 26 | + type: 'string', |
| 27 | + }, |
| 28 | + references: { |
| 29 | + elements: { |
| 30 | + additionalProperties: true, |
| 31 | + optionalProperties: { |
| 32 | + category: { |
| 33 | + type: 'string', |
| 34 | + }, |
| 35 | + references: { |
| 36 | + type: 'string', |
| 37 | + }, |
| 38 | + }, |
| 39 | + }, |
| 40 | + }, |
| 41 | + }, |
| 42 | + }, |
| 43 | + }, |
| 44 | +}) |
| 45 | + |
| 46 | +const validateSchema = ajv.compile(inputSchema) |
| 47 | + |
| 48 | +/** |
| 49 | + * If the document language is specified but not English, it MUST be tested that at least one item in document |
| 50 | + * references exists that starts with the language-specific translation of the term Superseding Document as summary. |
| 51 | + * The category of this item MUST be external. If no language-specific translation has been recorded, |
| 52 | + * the test MUST be skipped and output an information to the user that no such translation is known. |
| 53 | + * |
| 54 | + * @param {unknown} doc |
| 55 | + */ |
| 56 | +export function recommendedTest_6_2_39_4(doc) { |
| 57 | + /* |
| 58 | + The `ctx` variable holds the state that is accumulated during the test run and is |
| 59 | + finally returned by the function. |
| 60 | + */ |
| 61 | + const ctx = { |
| 62 | + warnings: |
| 63 | + /** @type {Array<{ instancePath: string; message: string }>} */ ([]), |
| 64 | + } |
| 65 | + |
| 66 | + const referenceCategory = 'external' |
| 67 | + |
| 68 | + if (!validateSchema(doc) || doc.document.category !== 'csaf_superseded') { |
| 69 | + return ctx |
| 70 | + } |
| 71 | + |
| 72 | + const supersedingInDocLang = getTranslationInDocumentLang( |
| 73 | + doc, |
| 74 | + 'superseding_document' |
| 75 | + ) |
| 76 | + if (!supersedingInDocLang) { |
| 77 | + ctx.warnings.push({ |
| 78 | + instancePath: '/document/references', |
| 79 | + message: |
| 80 | + 'no language specific translation for "Superseding Document" has been recorded', |
| 81 | + }) |
| 82 | + return ctx |
| 83 | + } |
| 84 | + |
| 85 | + if (isLangSpecifiedAndNotEnglish(doc.document.lang)) { |
| 86 | + const references = doc.document.references |
| 87 | + if ( |
| 88 | + !references || |
| 89 | + !existsReferenceWithSummaryAndCategory( |
| 90 | + references, |
| 91 | + supersedingInDocLang, |
| 92 | + referenceCategory |
| 93 | + ) |
| 94 | + ) { |
| 95 | + ctx.warnings.push({ |
| 96 | + instancePath: '/document/references', |
| 97 | + message: |
| 98 | + `for document category "csaf_superseded" at least one references must exist ` + |
| 99 | + `with reference category "${referenceCategory}" and whose summary begins with ${supersedingInDocLang}`, |
| 100 | + }) |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + return ctx |
| 105 | +} |
0 commit comments