-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvalidate-sector-schemas.js
63 lines (52 loc) · 2.03 KB
/
validate-sector-schemas.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
// This routine is used to validate the set schema files
// It utilises the ajv validator and will pick up most inconsistencies
// Author: Tomas Schier
const Ajv = require("ajv")
var fs = require('fs');
var path = require('path');
const addFormats = require("ajv-formats");
const { Console } = require("console");
const sectors = ['banking', 'energy', 'energy_sdh', 'register', 'admin', 'dcr'];
const version = '1.29.1';
sectors.forEach(sector => {
const directoryPath = path.join(__dirname, version + '/schemas/' + sector);
const commonDirectoryPath = path.join(__dirname, version + '/schemas/common');
var dsbSchemas = [];
// Read the common schemas unless reading the register api
if (sector != 'register' || sector != 'admin' || sector != 'dcr') {
var commonFiles = fs.readdirSync(commonDirectoryPath);
commonFiles.forEach(function (file) {
var filePath = path.join(commonDirectoryPath, file);
var data = JSON.parse(fs.readFileSync(filePath));
//var fileName = file.substr(0, file.indexOf('.'));
data.$id = 'common/' + file;
dsbSchemas.push(data);
});
console.log("Added Common for " + sector + " validation");
}
// Read the schemas to be validated
var files = fs.readdirSync(directoryPath);
files.forEach(function (file) {
var filePath = path.join(directoryPath, file);
var data = JSON.parse(fs.readFileSync(filePath));
//var fileName = file.substr(0, file.indexOf('.'));
data.$id = file;
dsbSchemas.push(data);
});
var ajv = new Ajv({strictSchema: false});
addFormats(ajv);
// Configure the validator by adding the schema arrays
var validate = ajv.addSchema(dsbSchemas);
files.forEach(file => {
var filePath = path.join(directoryPath, file);
//console.log('Processing file: ' + filePath);
var data = JSON.parse(fs.readFileSync(filePath));
try {
validate.compile(data);
}catch(e) {
console.log('ERROR in file ' + file + ': ' + e.message);
}
});
console.log("Validated " + sector);
console.log("All done for " + sector)
});