-
Notifications
You must be signed in to change notification settings - Fork 8
/
mandatoryTest_6_1_24.js
69 lines (66 loc) · 2.11 KB
/
mandatoryTest_6_1_24.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
* @param {unknown} doc
*/
export default function mandatoryTest_6_1_24(doc) {
/** @type {Array<{ message: string; instancePath: string }>} */
const errors = []
let isValid = true
// 6.1.24 Definition in Involvements
if (preconditionFor_6_1_24_Matches(doc)) {
doc.vulnerabilities.forEach((vulnerability, vulnerabilityIndex) => {
/** @type {Map<string, Set<string>>} */
const involvementMap = new Map()
vulnerability.involvements.forEach((involvement, involvementIndex) => {
if (
typeof involvement.date === 'string' &&
typeof involvement.party === 'string'
) {
const set = involvementMap.get(involvement.date) ?? new Set()
if (set.has(`${involvement.party}`)) {
isValid = false
errors.push({
message: `status for party was already given for the same date`,
instancePath: `/vulnerabilities/${vulnerabilityIndex}/involvements/${involvementIndex}`,
})
}
set.add(`${involvement.party}`)
involvementMap.set(involvement.date, set)
}
})
})
}
return { errors, isValid }
}
/**
* @param {unknown} rawDoc
* @returns {rawDoc is {
* vulnerabilities: Array<{
* involvements: Array<{
* date?: string
* party?: string
* status?: string
* }>
* }>
* }}
*/
const preconditionFor_6_1_24_Matches = (rawDoc) => {
if (typeof rawDoc !== 'object' || !rawDoc) return false
/** @type {{ vulnerabilities?: unknown }} */
const doc = rawDoc
return (
Array.isArray(doc.vulnerabilities) &&
doc.vulnerabilities.every(
(vulnerability) =>
Array.isArray(vulnerability.involvements) &&
vulnerability.involvements.every(
(/** @type {any} */ involvement) =>
(typeof involvement.date === 'string' ||
involvement.date === undefined) &&
(typeof involvement.party === 'string' ||
involvement.party === undefined) &&
(typeof involvement.status === 'string' ||
involvement.status === undefined)
)
)
)
}