forked from ankit-sharechat/Widget-JSON-Validator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
itemValidator.js
83 lines (68 loc) · 3.95 KB
/
itemValidator.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
const {ITEM_CARD, ITEM_STACK, validItemsConfigKeys} = require("./constants");
const {
error, getActionSource, getEventSource,
getDatasourceObject, getItemSource, validateKeys, onItemSourceReferred, dataSourceReferrerFormatter
} = require("./helpers");
const {validateSlot} = require("./slotValidators");
const {validateDataNode} = require("./dataValidator");
const {validateViewActionRef, validateClickActionRef} = require("./actionValidator");
function validateItems(widgetJson, items) {
items.forEach(itemReference => {
validateItemReference(widgetJson, itemReference, "items", 'items')
})
}
function validateItemSource(itemSource, itemRef, onFound) {
onItemSourceReferred(itemRef)
if (itemSource[itemRef] === undefined) {
error("ItemDefinitionNotFound: `" + itemRef + "` in itemSource")
return
}
onFound(itemSource[itemRef])
}
function validateItemCard(widgetJson, itemDefinition, dataObject, referrer, dataNode) {
validateSlot(widgetJson, itemDefinition.topStart, dataObject, referrer + ".topStart", dataNode)
validateSlot(widgetJson, itemDefinition.topCenter, dataObject, referrer + ".topCenter", dataNode)
validateSlot(widgetJson, itemDefinition.topEnd, dataObject, referrer + ".topEnd", dataNode)
validateSlot(widgetJson, itemDefinition.bottomCenter, dataObject, referrer + ".bottomCenter", dataNode)
validateSlot(widgetJson, itemDefinition.center, dataObject, referrer + ".center", dataNode)
validateSlot(widgetJson, itemDefinition.bottomStart, dataObject, referrer + ".bottomStart", dataNode)
validateSlot(widgetJson, itemDefinition.bottomEnd, dataObject, referrer + ".bottomEnd", dataNode)
validateSlot(widgetJson, itemDefinition.content, dataObject, referrer + ".content", dataNode)
}
function validateItemDefinition(widgetJson, itemDefinition, dataSource, referrer, dataNode) {
if (itemDefinition.type === ITEM_CARD) {
validateItemCard(widgetJson, itemDefinition, dataSource, referrer, dataNode)
} else if (itemDefinition.type === ITEM_STACK) {
const topDataNode = dataNode + ".top"
if (validateDataNode(dataSource, topDataNode, dataSourceReferrerFormatter(topDataNode) + referrer + ".top", topDataNode)) {
validateItemCard(widgetJson, itemDefinition.top, dataSource, referrer + ".top", topDataNode)
}
const bottomDataNode = dataNode + ".bottom"
if (validateDataNode(dataSource, bottomDataNode, dataSourceReferrerFormatter(bottomDataNode) + referrer + ".bottom", bottomDataNode)) {
validateItemCard(widgetJson, itemDefinition.bottom, dataSource, referrer + ".bottom", bottomDataNode)
}
}
}
function validateItemReference(widgetJson, itemReference, referrer, dataNode) {
if (itemReference === undefined)
return
const itemRef = itemReference.itemRef
const newDataNode = dataNode + "." + itemReference.dataRef
const clickActionRef = itemReference.cActionRef
const viewActionRef = itemReference.vActionRef
referrer = dataSourceReferrerFormatter(newDataNode) + "Node: " + referrer + "." + itemRef
validateItemSource(getItemSource(widgetJson), itemRef, itemDefinition => {
const dataPresent = validateDataNode(getDatasourceObject(widgetJson), newDataNode, referrer)
if (dataPresent) {
validateItemDefinition(widgetJson, itemDefinition, getDatasourceObject(widgetJson), referrer, newDataNode)
}
})
validateViewActionRef(getActionSource(widgetJson), getEventSource(widgetJson), getDatasourceObject(widgetJson), viewActionRef, referrer, newDataNode)
validateClickActionRef(getActionSource(widgetJson), getEventSource(widgetJson), getDatasourceObject(widgetJson), clickActionRef, referrer, newDataNode)
}
function validateItemsConfig(itemsConfig, referrer) {
if (itemsConfig === undefined)
return
validateKeys(Object.keys(itemsConfig), validItemsConfigKeys, referrer)
}
module.exports = {validateItems, validateItemReference, validateItemsConfig}