|
1 |
| -var glob = require("glob"); |
| 1 | +const glob = require("glob"); |
2 | 2 | const fs = require("fs");
|
3 | 3 | const Ajv = require("ajv");
|
4 | 4 | const chai = require("chai");
|
5 | 5 | const axios = require("axios");
|
6 |
| -const assert = chai.assert; |
| 6 | + |
| 7 | +const { assert } = chai; |
7 | 8 |
|
8 | 9 | function loadRemoteSchema(uri) {
|
9 |
| - return axios.get(uri).then(function (res) { |
10 |
| - if (res.status >= 400) { |
11 |
| - throw new Error("Schema loading error: " + res.statusCode); |
12 |
| - } |
13 |
| - return res.data; |
| 10 | + const client = axios.create({ |
| 11 | + timeout: 1500, |
14 | 12 | });
|
| 13 | + |
| 14 | + return client |
| 15 | + .get(uri) |
| 16 | + .catch((e) => { |
| 17 | + console.log(`error while loading schema from ${uri} with error ${e}`); |
| 18 | + }) |
| 19 | + .then((res) => { |
| 20 | + if (res.status >= 400) { |
| 21 | + throw new Error(`Schema loading error: ${res.statusCode}`); |
| 22 | + } |
| 23 | + return res.data; |
| 24 | + }); |
15 | 25 | }
|
16 | 26 |
|
17 |
| -const validateSchema = (filepath) => { |
18 |
| - return new Promise((resolve, reject) => { |
19 |
| - fs.readFile(filepath, "utf8", function (err, fileContents) { |
| 27 | +const validateSchema = (filepath) => |
| 28 | + new Promise((resolve, reject) => { |
| 29 | + fs.readFile(filepath, "utf8", (err, fileContents) => { |
20 | 30 | if (err) {
|
21 | 31 | reject(err);
|
22 | 32 | } else {
|
23 |
| - var schema = JSON.parse(fileContents); |
| 33 | + const schema = JSON.parse(fileContents); |
| 34 | + |
24 | 35 | const ajv = new Ajv({
|
25 | 36 | allErrors: true,
|
26 | 37 | loadSchema: loadRemoteSchema,
|
27 | 38 | });
|
28 |
| - ajv.compileAsync(schema, (err, validate) => { |
29 |
| - if (err) { |
30 |
| - resolve({ isValid: false, filepath: filepath, errors: ajv.errors }); |
| 39 | + |
| 40 | + ajv.compileAsync(schema, (compileErr) => { |
| 41 | + if (compileErr) { |
| 42 | + resolve({ isValid: false, filepath, errors: ajv.errors }); |
31 | 43 | } else {
|
32 |
| - resolve({ isValid: true, filepath: filepath }); |
| 44 | + resolve({ isValid: true, filepath }); |
33 | 45 | }
|
34 | 46 | });
|
35 | 47 | }
|
36 | 48 | });
|
37 | 49 | });
|
38 |
| -}; |
39 | 50 |
|
40 |
| -describe("validate schemas", function () { |
41 |
| - it("should validate all schemas", function (done) { |
42 |
| - const options = { absolute: true }; |
43 |
| - glob("**/*schema.json", options, function (err, files) { |
44 |
| - var promises = []; |
| 51 | +describe("validate schemas", () => { |
| 52 | + it("should validate all schemas", (done) => { |
| 53 | + const options = { absolute: true, ignore: "node_modules/**" }; |
| 54 | + glob("**/*schema.json", options, (err, files) => { |
| 55 | + if (err) { |
| 56 | + console.log(err); |
| 57 | + } |
| 58 | + const promises = []; |
45 | 59 | files.forEach((file) => {
|
46 | 60 | promises.push(validateSchema(file));
|
47 | 61 | });
|
48 |
| - Promise.all(promises).then((values) => { |
49 |
| - values.forEach((result) => { |
50 |
| - if (!result.isValid) { |
51 |
| - console.log(result.errors); |
52 |
| - } |
| 62 | + Promise.all(promises) |
| 63 | + .catch((promiseErr) => { |
| 64 | + console.log(promiseErr); |
| 65 | + }) |
| 66 | + .then((values) => { |
| 67 | + values.forEach((result) => { |
| 68 | + if (!result.isValid) { |
| 69 | + console.log(result.errors); |
| 70 | + } |
53 | 71 |
|
54 |
| - assert.isTrue(result.isValid, `${result.filepath} is invalid`); |
55 |
| - }); |
| 72 | + assert.isTrue(result.isValid, `${result.filepath} is invalid`); |
| 73 | + }); |
56 | 74 |
|
57 |
| - done(); |
58 |
| - }); |
| 75 | + done(); |
| 76 | + }); |
59 | 77 | });
|
60 |
| - }); |
| 78 | + }).timeout(20000); |
61 | 79 | });
|
0 commit comments