forked from ankit-sharechat/Widget-JSON-Validator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cssValidator.js
83 lines (77 loc) · 3.37 KB
/
cssValidator.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 {error, validateKeys, onCssSourceReferred} = require("./helpers");
const {
validCssType, BACKGROUND, PADDING, SIZE, FILLHEIGHT, FILLWIDTH, ALPHA, BORDER, ROTATE, ELEVATION, ASPECT_RATIO,
GRADIENT, validBackgroundKeys, validPaddingKeys, validSizeKeys, validfillMaxHeightKeys, validfillMaxWidthKeys,
validAlphaKeys, validBorderKeys, validRotateKeys, validElevationKeys, validAspectRatioKeys, validGradientKeys
} = require("./constants");
function validateCssRef(cssSource, cssRefs, referrer) {
if (cssSource === undefined) {
error("cssSource: missing!")
return
}
if (cssRefs !== undefined) {
//Validate Css Ref is a list
cssRefs.forEach(cssRef => {
onCssSourceReferred(cssRef)
if (cssSource[cssRef] === undefined) {
error("CssNotFound: `" + cssRef + "` in cssSource, Info: { " + referrer + " }")
} else {
validateCssDefinitionList(cssSource[cssRef], "Check: cssSource." + cssRef)
}
})
}
}
function validateBackgroundValues(cssDefinition, referrer) {
//todo: fill in
}
function validateCssDefinition(cssDefinition, referrer) {
if (!validCssType.includes(cssDefinition.type)) {
error("InvalidModifierType: `" + cssDefinition.type + "` It must be one of [" + validCssType + "]")
return
}
switch (cssDefinition.type) {
case BACKGROUND:
validateKeys(Object.keys(cssDefinition), validBackgroundKeys, referrer + "." + cssDefinition.type)
//todo: validate values
validateBackgroundValues(cssDefinition, referrer + "." + cssDefinition.type)
break
case PADDING:
validateKeys(Object.keys(cssDefinition), validPaddingKeys, referrer + "." + cssDefinition.type)
break
case SIZE:
validateKeys(Object.keys(cssDefinition), validSizeKeys, referrer + "." + cssDefinition.type)
break
case FILLHEIGHT:
validateKeys(Object.keys(cssDefinition), validfillMaxHeightKeys, referrer + "." + cssDefinition.type)
break
case FILLWIDTH:
validateKeys(Object.keys(cssDefinition), validfillMaxWidthKeys, referrer + "." + cssDefinition.type)
break
case ALPHA:
validateKeys(Object.keys(cssDefinition), validAlphaKeys, referrer + "." + cssDefinition.type)
break
case BORDER:
validateKeys(Object.keys(cssDefinition), validBorderKeys, referrer + "." + cssDefinition.type)
break
case ROTATE:
validateKeys(Object.keys(cssDefinition), validRotateKeys, referrer + "." + cssDefinition.type)
break
case ELEVATION:
validateKeys(Object.keys(cssDefinition), validElevationKeys, referrer + "." + cssDefinition.type)
break
case ASPECT_RATIO:
validateKeys(Object.keys(cssDefinition), validAspectRatioKeys, referrer + "." + cssDefinition.type)
break
case GRADIENT:
validateKeys(Object.keys(cssDefinition), validGradientKeys, referrer + "." + cssDefinition.type)
break
}
}
function validateCssDefinitionList(cssDefinitionList, referrer) {
let index = 0
cssDefinitionList.forEach(cssDefinition => {
validateCssDefinition(cssDefinition, "Index:"+index+", "+referrer)
index++
})
}
module.exports = {validateCssRef}