-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
88 lines (75 loc) · 2.66 KB
/
index.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
const actionsSpec = require('./actions-spec.json')
const getRequiredKeys = actionSpec =>
actionSpec.filter(([_, { required }]) => required).map(([key]) => key)
const getKeyWithRegexps = actionSpec =>
actionSpec.filter(([_, { regexp }]) => regexp !== undefined)
const validateRequiredKeys = (action, actionSpec) =>
getRequiredKeys(actionSpec)
.filter(key => action[key] === undefined)
.map(key => ({
problem: `Missing '${key}'`
}))
const validateUnnecessaryKeys = (action, actionSpec) =>
Object
.keys(action)
.filter(key => key !== 'type' && !actionSpec.map(([key]) => key).includes(key))
.map(key => ({
problem: `Unnecessary '${key}'`
}))
const validateAction = (action, path, verbose = false) => {
if (verbose) console.log('validating action', path)
if (actionsSpec[action.type] === undefined) {
return [
{
path,
type: action.type,
problem: `Unrecognized action type '${action.type}'`
}
]
}
const regexpErrors = getKeyWithRegexps(actionsSpec[action.type].keys)
.map(([key, { regexp }]) => {
if (action[key]) {
if ((new RegExp(regexp)).test(action[key])) {
return undefined
} else {
return {
problem: `Value '${action[key]}' for '${key}' does not match the spec format`,
regexp
}
}
}
return undefined
})
.filter(x => x !== undefined)
return [
...validateRequiredKeys(action, actionsSpec[action.type].keys),
...(actionsSpec[action.type].allowUnknownKeys === true
? []
: validateUnnecessaryKeys(action, actionsSpec[action.type].keys)),
...regexpErrors
]
.map(error => ({
...error,
path,
type: action.type
}))
}
const validateStep = (step, path, verbose = false) =>
(verbose ? console.log('validating step', path) : undefined, true) &&
step.actions.map((action, index) =>
validateAction(action, [...path, 'actions', index], verbose)
)
.filter((x) => x.length > 0)
.reduce((a, b) => a.concat(b), [])
const validateConfig = (config, verbose = false) =>
(verbose ? console.log('validating config') : undefined, true) &&
config.steps.map((step, index) =>
validateStep(step, ['steps', index], verbose)
)
.reduce((a, b) => a.concat(b), [])
module.exports = {
validateAction,
validateStep,
validateConfig
}