-
Notifications
You must be signed in to change notification settings - Fork 8
/
mandatoryTest_6_1_23.js
49 lines (45 loc) · 1.26 KB
/
mandatoryTest_6_1_23.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
* @param {unknown} doc
*/
export default function mandatoryTest_6_1_23(doc) {
/** @type {Array<{ message: string; instancePath: string }>} */
const errors = []
let isValid = true
// 6.1.23 Multiple Use of Same CVE
if (preconditionFor_6_1_23_Matches(doc)) {
/** @type {Set<string>} */
const cveStrings = new Set()
doc.vulnerabilities.forEach((vulnerability, vulnerabilityIndex) => {
if (vulnerability.cve === undefined) return
if (cveStrings.has(vulnerability.cve)) {
isValid = false
errors.push({
message: `CVE identifier was already used`,
instancePath: `/vulnerabilities/${vulnerabilityIndex}/cve`,
})
}
cveStrings.add(vulnerability.cve)
})
}
return { errors, isValid }
}
/**
* @param {unknown} rawDoc
* @returns {rawDoc is {
* vulnerabilities: Array<{
* cve?: string
* }>
* }}
*/
const preconditionFor_6_1_23_Matches = (rawDoc) => {
if (typeof rawDoc !== 'object' || !rawDoc) return false
/** @type {{ vulnerabilities?: unknown }} */
const doc = rawDoc
return (
Array.isArray(doc.vulnerabilities) &&
doc.vulnerabilities.every(
(vulnerability) =>
typeof vulnerability.cve === 'string' || vulnerability.cve === undefined
)
)
}