-
Notifications
You must be signed in to change notification settings - Fork 8
/
mandatoryTest_6_1_14.js
48 lines (43 loc) · 1.36 KB
/
mandatoryTest_6_1_14.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
import semver from 'semver'
const { gt, valid } = semver
/**
* @param {unknown} doc
*/
export default function mandatoryTest_6_1_14(doc) {
/** @type {Array<{ message: string; instancePath: string }>} */
const errors = []
let isValid = true
if (preconditionMatches(doc)) {
const sortedNumbers = Array.from(
new Set(
doc.document.tracking.revision_history
.slice()
.sort(
(a, z) => new Date(a.date).getTime() - new Date(z.date).getTime()
)
.map((e) => valid(e.number) ?? `${e.number}.0.0`)
).keys()
).filter((n) => valid(n) !== null)
const isAscending = sortedNumbers.every(
(number, index, all) => index === 0 || gt(number, all[index - 1])
)
if (!isAscending) {
isValid = false
errors.push({
instancePath: `/document/tracking/revision_history`,
message: 'the items must be in ascending order according to the date',
})
}
}
return { errors, isValid }
}
/**
* @param {any} doc
* @returns {doc is { document: { tracking: { revision_history: Array<{ number: string; date: string }> } } }}
*/
const preconditionMatches = (doc) =>
Array.isArray(doc?.document?.tracking?.revision_history) &&
doc.document.tracking.revision_history.every(
(/** @type {any} */ r) =>
typeof r.number === 'string' && typeof r.date === 'string'
)