forked from taskcluster/taskcluster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate.js
140 lines (123 loc) · 4.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
const debug = require('debug')('taskcluster-lib-validate');
const _ = require('lodash');
const fs = require('fs');
const path = require('path');
const walk = require('walk');
const yaml = require('js-yaml');
const assert = require('assert');
const Ajv = require('ajv');
const libUrls = require('taskcluster-lib-urls');
const { renderConstants, checkRefs } = require('./util/validate_util');
const rootdir = require('app-root-dir');
const ABSTRACT_SCHEMA_ROOT_URL = '';
class SchemaSet {
constructor(options) {
assert(options.serviceName, 'A `serviceName` must be provided to taskcluster-lib-validate!');
this._schemas = {};
const defaultFolder = path.join(rootdir.get(), 'schemas');
this.cfg = _.defaults(options, {
folder: defaultFolder,
constants: path.join(options && options.folder || defaultFolder, 'constants.yml'),
});
if (_.isString(this.cfg.constants)) {
const fullpath = path.resolve(this.cfg.constants);
debug('Attempting to set constants by file: %s', fullpath);
try {
this.cfg.constants = yaml.load(fs.readFileSync(fullpath, 'utf-8'));
} catch (err) {
if (err.code === 'ENOENT') {
debug('Constants file does not exist, setting constants to {}');
this.cfg.constants = {};
} else {
throw err;
}
}
}
let walkErr;
walk.walkSync(path.resolve(this.cfg.folder), { listeners: { file: (root, stats) => {
try {
let name = path.relative(this.cfg.folder, path.join(root, stats.name));
let json = null;
const data = fs.readFileSync(path.join(this.cfg.folder, name), 'utf-8');
if (/\.ya?ml$/.test(name) && name !== 'constants.yml') {
json = yaml.load(data);
} else if (/\.json$/.test(name)) {
json = JSON.parse(data);
} else {
debug('Ignoring file %s', name);
return;
}
const jsonName = name.replace(/\.ya?ml$/, '.json');
const schema = renderConstants(json, this.cfg.constants);
checkRefs(schema, this.cfg.serviceName);
this._schemas[jsonName] = schema;
} catch (err) {
// walk swallows errors, so we must raise them ourselves
if (!walkErr) {
walkErr = err;
}
}
} } });
if (walkErr) {
throw walkErr;
}
debug('finished walking tree of schemas');
}
_schemaWithIds(rootUrl) {
return _.mapValues(this._schemas, (schema, jsonName) => {
const newSchema = _.clone(schema);
newSchema.$id = libUrls.schema(rootUrl, this.cfg.serviceName, jsonName + '#');
// rewrite a relative `/schemas/<service>/<path>..` URI to point to a full URL
const match = /^\/schemas\/([^/]*)\/(.*)$/.exec(newSchema.$schema);
if (match) {
newSchema.$schema = libUrls.schema(rootUrl, match[1], match[2]);
}
return newSchema;
});
}
abstractSchemas() {
return this._schemaWithIds(ABSTRACT_SCHEMA_ROOT_URL);
}
absoluteSchemas(rootUrl) {
return this._schemaWithIds(rootUrl);
}
async validator(rootUrl) {
const ajv = Ajv({
useDefaults: true,
format: 'full',
verbose: true,
// schema validation occurs in the tests and need not be done here
validateSchema: false,
allErrors: true,
});
ajv.addMetaSchema(require('ajv/lib/refs/json-schema-draft-06.json'));
_.forEach(this.absoluteSchemas(rootUrl), schema => {
ajv.addSchema(schema);
});
return (obj, id) => {
id = id.replace(/#$/, '');
id = id.replace(/\.ya?ml$/, '.json');
if (!_.endsWith(id, '.json')) {
id += '.json';
}
id += '#';
ajv.validate(id, obj);
if (ajv.errors) {
_.forEach(ajv.errors, function(error) {
if (error.params['additionalProperty']) {
error.message += ': ' + JSON.stringify(error.params['additionalProperty']);
}
});
return [
'\nSchema Validation Failed!',
'\nRejecting Schema: ',
id,
'\nErrors:\n * ',
ajv.errorsText(ajv.errors, { separator: '\n * ' }),
].join('');
}
return null;
};
}
}
module.exports = SchemaSet;