-
Notifications
You must be signed in to change notification settings - Fork 8
/
mandatoryTest_6_1_3.js
94 lines (85 loc) · 2.45 KB
/
mandatoryTest_6_1_3.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
/**
* @typedef {object} FullProductName
* @property {unknown} product_id
*/
/**
* @typedef {object} Relationship
* @property {unknown} relates_to_product_reference
* @property {unknown} product_reference
* @property {FullProductName | null} [full_product_name]
*/
/**
* @param {any} doc
*/
export default function mandatoryTest_6_1_3(doc) {
/** @type {Array<{ message: string; instancePath: string }>} */
const errors = []
let isValid = true
if (!Array.isArray(doc.product_tree?.relationships)) {
return { isValid, errors }
}
/** @type {Array<Relationship>} */
const relationships = doc.product_tree.relationships
relationships.forEach((_, index) => {
/** @type {Set<string>} */
const erroredAttributeSet = new Set()
search([], relationships, index, null, ({ key }) => {
if (typeof key === 'string' && !erroredAttributeSet.has(key)) {
erroredAttributeSet.add(key)
isValid = false
errors.push({
instancePath: `/product_tree/relationships/${index}/${key}`,
message: 'circular reference',
})
}
})
})
return { isValid, errors }
}
/**
* @param {number[]} path
* @param {Relationship[]} relationships
* @param {number} index
* @param {string | null} key
* @param {(params: { key: string | null }) => void} onCycle
* @returns
*/
function search(path, relationships, index, key, onCycle) {
const relationship = relationships[index]
if (
typeof relationship.full_product_name?.product_id === 'string' &&
path.includes(index)
) {
return onCycle({ key })
}
if (typeof relationship.product_reference === 'string') {
const productRelationshipIndex = relationships.findIndex(
(r) => r.full_product_name?.product_id === relationship.product_reference
)
if (productRelationshipIndex !== -1) {
search(
[...path, index],
relationships,
productRelationshipIndex,
key ?? 'product_reference',
onCycle
)
}
}
if (typeof relationship.relates_to_product_reference === 'string') {
const relatesToProductRelationshipIndex = relationships.findIndex(
(r) =>
r.full_product_name?.product_id ===
relationship.relates_to_product_reference
)
if (relatesToProductRelationshipIndex !== -1) {
search(
[...path, index],
relationships,
relatesToProductRelationshipIndex,
key ?? 'relates_to_product_reference',
onCycle
)
}
}
}