-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvalidate.js
71 lines (60 loc) · 2.14 KB
/
validate.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
const fs = require("fs");
const path = require("path");
const config = require("./src/behaviorConfig.js");
const { exit } = require("process");
function validateDisplayText(name, obj) {
// check if obj has displayText property
if (obj.hasOwnProperty("displayText")) {
const paramCount = obj.params.length;
// use regex to find the number of {/d} in displayText
const regex = /{\d}/g;
const matches = obj.displayText.match(regex);
const matchCount = matches ? matches.length : 0;
// check if the number of {/d} matches the number of params
if (paramCount !== matchCount) {
console.log(
`Error: ${name} has ${matchCount} {x} in displayText but has ${paramCount} params`
);
return false; // validation failed
}
}
return true; // validation passed
}
const validationPipeline = [
validateDisplayText
];
function validateBehavior(behaviorConfig) {
//assume file is valid
const validations = [];
// iterate over all the actions
Object.keys(behaviorConfig.Acts).forEach((key) => {
const action = behaviorConfig.Acts[key];
validationPipeline.forEach((validationFunction) => {
validations.push(validationFunction(key, action));
});
});
// iterate over all the conditions
Object.keys(behaviorConfig.Cnds).forEach((key) => {
const condition = behaviorConfig.Cnds[key];
validationPipeline.forEach((validationFunction) => {
validations.push(validationFunction(key, condition));
});
});
// iterate over all the expressions
Object.keys(behaviorConfig.Exps).forEach((key) => {
const expression = behaviorConfig.Exps[key];
validationPipeline.forEach((validationFunction) => {
validations.push(validationFunction(key, expression));
});
});
// check if any validation failed
return validations.every((validation) => validation);
}
// check if the file is valid
if (validateBehavior(config)) {
console.info("Validation Passed");
exit(0);
} else {
console.error("Validation Failed");
exit(1);
}