-
Notifications
You must be signed in to change notification settings - Fork 8
/
mandatoryTest_6_1_31.js
99 lines (90 loc) · 2.27 KB
/
mandatoryTest_6_1_31.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import Ajv from 'ajv/dist/jtd.js'
const ajv = new Ajv()
const inputSchema = /** @type {const} */ ({
additionalProperties: true,
properties: {
product_tree: {
additionalProperties: true,
properties: {
branches: {
elements: {
additionalProperties: true,
properties: {},
},
},
},
},
},
})
const branchSchema = /** @type {const} */ ({
additionalProperties: true,
optionalProperties: {
category: { type: 'string' },
name: { type: 'string' },
branches: {
elements: {
additionalProperties: true,
properties: {},
},
},
},
})
const validate = ajv.compile(inputSchema)
const validateBranch = ajv.compile(branchSchema)
/**
* @param {any} doc
*/
export default function mandatoryTest_6_1_31(doc) {
const ctx = {
errors:
/** @type {Array<{ instancePath: string; message: string }>} */ ([]),
isValid: true,
}
if (!validate(doc)) {
return ctx
}
/**
* @param {object} params
* @param {string} params.path
* @param {unknown[]} params.branches
*/
function checkBranches({ path, branches }) {
branches.forEach((branch, branchIndex) => {
if (validateBranch(branch)) {
if (
branch.category === 'product_version' &&
typeof branch.name === 'string' &&
(['<', '<=', '>', '>='].some((str) =>
branch.name?.toLowerCase().includes(str)
) ||
['after', 'all', 'before', 'earlier', 'later', 'prior', 'versions'].some(
(str) =>
branch.name
?.toLowerCase()
.split(/\s/)
.some((word) => {
return str === word
})
))
) {
ctx.isValid = false
ctx.errors.push({
instancePath: `${path}/${branchIndex}/name`,
message: 'version range in product version',
})
}
if (Array.isArray(branch.branches)) {
checkBranches({
path: `${path}/${branchIndex}/branches`,
branches: branch.branches,
})
}
}
})
}
checkBranches({
path: '/product_tree/branches',
branches: doc.product_tree.branches,
})
return ctx
}