forked from ankit-sharechat/Widget-JSON-Validator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.js
171 lines (143 loc) · 3.73 KB
/
helpers.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
const {widgetCriticalField, DataSourceKey} = require("./constants");
let errors = []
let itemsReferred = []
let dataReferred = []
let cssReferred = []
let eventReferred = []
let actionReferred = []
function logError(msg) {
//console.log('\x1b[31m', msg); //Red
errors.push(msg)
}
function ok(msg) {
console.log('\x1b[32m', msg); //Green
}
function printErrors(errors) {
console.log('\x1b[31m', "=========== All Errors ==========="); //Red
console.log('\x1b[31m', "Total Errors: " + errors.length); //Red
console.log('\x1b[31m', "==================================="); //Red
errors.forEach(error => {
console.log('\x1b[31m', error); //Red
})
}
function printOkMessage() {
console.log('\x1b[32m', " =======================================");
console.log('\x1b[32m', "| |");
console.log('\x1b[32m', "| All Good! : 👍 |");
console.log('\x1b[32m', "| |");
console.log('\x1b[32m', " =======================================");
}
function getDatasourceObject(json) {
return json.dataSource
}
function getCssSource(json) {
if (json.cssSource === undefined) {
fieldMissing('cssSource')
}
return json.cssSource
}
function getEventSource(json) {
if (json.eventSource === undefined) {
fieldMissing('eventSource')
}
return json.eventSource
}
function getItemSource(json) {
return json.template.itemSource
}
function getActionSource(json) {
if (json.actionSource === undefined) {
fieldMissing('actionSource')
}
return json.actionSource
}
function fieldMissing(fieldName) {
logError("`" + fieldName + "` is Missing!")
}
function validateKeys(keys, validKeys, referrer) {
keys.forEach(key => {
if (!validKeys.includes(key)) {
logError("Invalid Key: " + key + ", It must be one of [" + validKeys + "] | Info: " + referrer)
}
})
}
function onItemSourceReferred(key) {
if (!itemsReferred.includes(key)) {
itemsReferred.push(key)
}
}
function dataSourceReferrerFormatter(dataRef) {
return DataSourceKey + ": "+dataRef+" |"
}
function onDataSourceReferred(key) {
dataReferred.push(key)
}
function onCssSourceReferred(key) {
if (!cssReferred.includes(key)) {
cssReferred.push(key)
}
}
function onEventSourceReferred(key) {
if (!eventReferred.includes(key)) {
eventReferred.push(key)
}
}
function onActionSourceReferred(key) {
if (!actionReferred.includes(key)) {
actionReferred.push(key)
}
}
function getActionsReferred() {
return actionReferred
}
function getEventsReferred() {
return eventReferred
}
function getCssReferred() {
return cssReferred
}
function getDataReferred() {
return dataReferred
}
function getItemsReferred() {
return itemsReferred
}
function criticalFieldPresent(json) {
const keysPresent = Object.keys(json)
let allFieldPresent = true
widgetCriticalField.forEach(field => {
if (!keysPresent.includes(field)) {
fieldMissing(field)
allFieldPresent = false
}
})
return allFieldPresent
}
function getAllErrors() {
return errors
}
module.exports = {
error: logError,
getDatasourceObject,
getCssSource,
getEventSource,
getItemSource,
getActionSource,
validateKeys,
criticalFieldPresent,
getAllErrors,
printErrors,
printOkMessage,
fieldMissing,
onItemSourceReferred,
onActionSourceReferred,
onEventSourceReferred,
onDataSourceReferred,
onCssSourceReferred,
getItemsReferred,
getActionsReferred,
getEventsReferred,
getCssReferred,
getDataReferred,
dataSourceReferrerFormatter
}