forked from ankit-sharechat/Widget-JSON-Validator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataValidator.js
46 lines (42 loc) · 1.6 KB
/
dataValidator.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
const {error, onDataSourceReferred} = require("./helpers");
function validateDataNode(dataSource, dataNode, referrer) {
const nodes = dataNode.split(".")
if (dataSource !== undefined) {
onDataSourceReferred(dataNode)
let dataObject = dataSource
let foundTill = ""
nodes.forEach(node => {
if (dataObject[node] !== undefined) {
dataObject = dataObject[node]
foundTill = (foundTill.length === 0 ? "" : foundTill + ".") + node
} else {
error("DataNotFound: `" + foundTill + "." + node + "` in dataSource. Info: " + referrer)
}
})
return foundTill === dataNode
}
}
function getDataAtNode(dataSource, dataNode) {
const nodes = dataNode.split(".")
if (dataSource !== undefined) {
let dataObject = dataSource
nodes.forEach(node => {
if (dataObject[node] !== undefined) {
dataObject = dataObject[node]
}
})
return dataObject
}
}
function validatePlaceHolder(jsonObject, dataSource, referrer, dataNode) {
const keys = Object.keys(jsonObject)
keys.forEach(key => {
const isPlaceholder = jsonObject[key].toString().startsWith("@")
if (isPlaceholder) {
const placeholderKey = jsonObject[key].substring(1)
const newDataNode = dataNode + "." + placeholderKey
validateDataNode(dataSource, newDataNode, "(Placehodler cannot be filled for-> " + placeholderKey + ") " + referrer)
}
})
}
module.exports = {validatePlaceHolder, validateDataNode, getDataAtNode}