forked from ankit-sharechat/Widget-JSON-Validator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slotValidators.js
199 lines (174 loc) · 7.72 KB
/
slotValidators.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
const {
SLOT_TXT,
SLOT_2_LINE,
SLOT_LOT,
SLOT_IMG,
SLOT_PLAY,
SLOT_CHIP,
validSlotTypes,
validTextObjectKeys, validImageObjectKeys, validTwoLineTextSlotKeys, validPlayableObjectKeys, validChipObjectKeys
} = require("./constants");
const {getCssSource, getEventSource, getActionSource, error, validateKeys} = require("./helpers");
const {validateDataNode} = require("./dataValidator");
const {validateColor} = require("./colorValidator");
const {validateStyle} = require("./styleValidator");
const {validateCssRef} = require("./cssValidator");
const {validateClickActionRef} = require("./actionValidator");
//todo:
function validateSlot(widgetJson, slot, dataSource, referrer, dataNode) {
if (slot === undefined)
return
const type = slot.type
validateSlotType(type, referrer)
validateSlotDefinition(widgetJson, slot, dataSource, referrer, dataNode)
}
function validateSlotType(slotType, referrer) {
if (!validSlotTypes.includes(slotType)) {
error("Invalid Slot Type:" + slotType + ", It must be one of [" + validSlotTypes + "], Tree: " + referrer)
}
}
function validateSlotDefinition(widgetJson, slot, dataSource, referrer, dataNode) {
switch (slot.type) {
case SLOT_TXT :
validateTextWidget(slot.text, dataSource, getCssSource(widgetJson), getEventSource(widgetJson), getActionSource(widgetJson), referrer + "." + SLOT_TXT, dataNode)
break
case SLOT_2_LINE :
validateSlot2Line(slot, dataSource, getCssSource(widgetJson), getEventSource(widgetJson), getActionSource(widgetJson), referrer + "." + SLOT_2_LINE, dataNode)
break
case SLOT_LOT:
validateLottieWidget(slot.lottie, dataSource, getCssSource(widgetJson), getEventSource(widgetJson), getActionSource(widgetJson), referrer + "." + SLOT_LOT, dataNode)
break
case SLOT_IMG:
validateImageWidget(slot.image, dataSource, getCssSource(widgetJson), getEventSource(widgetJson), getActionSource(widgetJson), referrer + "." + SLOT_IMG, dataNode)
break
case SLOT_PLAY:
validatePlayableWidget(slot.playable, dataSource, getCssSource(widgetJson), getEventSource(widgetJson), getActionSource(widgetJson), referrer + "." + SLOT_PLAY, dataNode)
break
case SLOT_CHIP:
validateChipWidget(slot.chip, dataSource, getCssSource(widgetJson), getEventSource(widgetJson), getActionSource(widgetJson), referrer + "." + SLOT_CHIP, dataNode)
break
default:
break
}
}
function validateSlot2Line(slot, dataSource, cssSource, eventSource, actionSource, referrer, dataNode) {
const topText = slot.top
const bottomText = slot.bottom
validateKeys(Object.keys(slot), validTwoLineTextSlotKeys, referrer)
if (topText !== undefined) {
validateTextWidget(topText, dataSource, cssSource, eventSource, actionSource, referrer + ".top", dataNode)
}
if (bottomText !== undefined) {
validateTextWidget(bottomText, dataSource, cssSource, eventSource, actionSource, referrer + ".bottom", dataNode)
}
}
function validateChipWidget(chipWidget, dataSource, cssSource, eventSource, actionSource, referrer, dataNode) {
if (chipWidget === undefined) {
error("`chip` Object is missing " + referrer)
return
}
validateKeys(Object.keys(chipWidget), validChipObjectKeys, referrer)
//Image Data
validateDataNode(dataSource, dataNode + "." + chipWidget.imageRef, referrer)
//Text Data
validateDataNode(dataSource, dataNode + "." + chipWidget.textRef, referrer)
//Color
validateColor(chipWidget.color, referrer)
//Style
validateStyle(chipWidget.style, referrer)
//Css Refs
validateCssRef(cssSource, chipWidget.cssRefs, referrer)
//Action Ref
validateClickActionRef(actionSource, eventSource, dataSource, chipWidget.cActionRef, referrer, dataNode)
}
function validatePlayableWidget(playableWidget, dataSource, cssSource, eventSource, actionSource, referrer, dataNode) {
if (playableWidget === undefined) {
error("`playable` Object is missing " + referrer)
return
}
validateKeys(Object.keys(playableWidget), validPlayableObjectKeys, referrer)
//todo: fix validation for playable slots
//DataSources
if (playableWidget.gifRef !== undefined) {
const newGifDataNode = dataNode + "." + playableWidget.gifRef
validateDataNode(dataSource, newGifDataNode, referrer)
}
if (playableWidget.videoRef !== undefined) {
const newVideoDataNode = dataNode + "." + playableWidget.videoRef
validateDataNode(dataSource, newVideoDataNode, referrer)
}
if (playableWidget.thumbRef !== undefined) {
const newThumbDataNode = dataNode + "." + playableWidget.thumbRef
validateDataNode(dataSource, newThumbDataNode, referrer)
}
}
function validateImageWidget(imageObject, dataSource, cssSource, eventSource, actionSource, referrer, dataNode) {
if (imageObject === undefined) {
error("`image` Object is missing " + referrer)
return
}
validateKeys(Object.keys(imageObject), validImageObjectKeys, referrer)
//DataSource
const newDataNode = dataNode + "." + imageObject.dataRef
validateDataNode(dataSource, newDataNode, referrer)
//Css Refs
validateCssRef(cssSource, imageObject.cssRefs, referrer)
//Action Ref
validateClickActionRef(actionSource, eventSource, dataSource, imageObject.cActionRef, referrer, dataNode)
}
function validateLottieWidget(lottieObject, dataSource, cssSource, eventSource, actionSource, referrer, dataNode) {
if (lottieObject === undefined) {
error("`lottie` Object is missing " + referrer)
return
}
validateKeys(Object.keys(lottieObject), validImageObjectKeys, referrer)
//DataSource
const newDataNode = dataNode + "." + lottieObject.dataRef
validateDataNode(dataSource, newDataNode, referrer)
//Css Refs
validateCssRef(cssSource, lottieObject.cssRefs, referrer)
//Action Ref
validateClickActionRef(actionSource, eventSource, dataSource, lottieObject.cActionRef, referrer, dataNode)
}
function validateTextWidget(textObject, dataSource, cssSource, eventSource, actionSource, referrer, dataNode) {
if (textObject === undefined) {
error("`text` Object is missing " + referrer)
return
}
const newDataNode = dataNode + "." + textObject.dataRef
validateKeys(Object.keys(textObject), validTextObjectKeys, referrer)
//DataSource
validateDataNode(dataSource, newDataNode, referrer)
//Color
validateColor(textObject.color, referrer)
//Style
validateStyle(textObject.style, referrer)
//Left Drawable
if (textObject.ld !== undefined) {
if (Object.keys(textObject.ld).length === 0) {
error("EmptyObject | " + referrer + ".ld")
return
}
validateDataNode(dataSource, dataNode + "." + textObject.ld.dataRef, referrer + ".ld")
validateCssRef(cssSource, textObject.ld.cssRefs, referrer + ".ld")
}
//Right Drawable
if (textObject.rd !== undefined) {
if (Object.keys(textObject.rd).length === 0) {
error("EmptyObject | " + referrer + ".rd")
return
}
validateDataNode(dataSource, dataNode + "." + textObject.rd.dataRef, referrer + ".rd")
validateCssRef(cssSource, textObject.rd.cssRefs, referrer + ".rd")
}
//Css Refs
if (textObject.cssRefs !== undefined) {
//Validate Css Ref is a list
validateCssRef(cssSource, textObject.cssRefs, referrer)
}
//Action Refs
if (textObject.cActionRef !== undefined) {
validateClickActionRef(actionSource, eventSource, dataSource, textObject.cActionRef, referrer, dataNode)
}
}
module.exports = {validateSlot}