forked from ankit-sharechat/Widget-JSON-Validator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwidgetValidator.js
35 lines (32 loc) · 1.38 KB
/
widgetValidator.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
const {validTemplateKeys, validTemplateTypes} = require("./constants");
const {
validateKeys, getCssSource, criticalFieldPresent, logError, fieldMissing, error
} = require("./helpers");
const {validateTemplateHeader} = require("./headerValidator");
const {validateItems, validateItemReference, validateItemsConfig} = require("./itemValidator");
const {validateCssRef} = require("./cssValidator");
function validateWidget(widgetJson) {
if (widgetJson !== undefined) {
const template = widgetJson.template
if (criticalFieldPresent(widgetJson)) {
validateKeys(Object.keys(template), validTemplateKeys, "template")
validateTemplateType(template.type)
validateTemplateHeader(widgetJson, template.header)
if (template.sctv !== undefined) {
validateItemReference(widgetJson, template.sctv, "sctv",'sctv')
}
validateItems(widgetJson, template.items)
validateItemsConfig(template.itemsConfig, "itemsConfig")
validateItemsConfig(template.sctvConfig, "sctvConfig")
validateCssRef(getCssSource(widgetJson), template.cssRefs, "template")
}
} else {
fieldMissing('genericWidget')
}
}
function validateTemplateType(type) {
if (!validTemplateTypes.includes(type)) {
error("Invalid Template Type: " + type)
}
}
module.exports = {validateWidget}