-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlint.js
79 lines (73 loc) · 2.51 KB
/
lint.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
const { Spectral, Document, Parsers, isOpenApiv2, isOpenApiv3 } = require("@stoplight/spectral");
const fs = require('fs');
const { join } = require('path');
const sampleOAS = [
'./examples/apple.yaml',
'./examples/cloudhome.v1.yaml'
];
// lints with default OAS ruleset
const apple = () => {
// Return the contents of sampleOAS as a string in the variable 'oas'
// "utf8" encodes the raw buffer data in human-readable format
fs.readFile(sampleOAS[0], 'utf8', (err, oas) => {
if (err) {
console.error(err);
}
else {
// Initialize spectral object from Spectral API
const spectral = new Spectral();
// Register supported OpenAPUI formats
spectral.registerFormat("oas2", isOpenApiv2);
spectral.registerFormat("oas3", isOpenApiv3);
// Load default OpenAPI ruleset
spectral.loadRuleset("spectral:oas")
// Lint the provided 'oas' object
.then(() => spectral.run(oas))
.then(results => {
// Writes the lint results to a file
fs.writeFile('./output/apple-results.json', JSON.stringify(results, null, 2), (err) => {
if (err) {
return console.error(err);
}
else {
console.log('OpenAPI description linted successfully at output/apple-results.json!');
}
});
});
}
});
};
// lints with default OAS ruleset
const cloudhome = () => {
// Return the contents of sampleOAS as a string in the variable 'oas'
// "utf8" encodes the raw buffer data in human-readable format
fs.readFile(sampleOAS[1], 'utf8', (err, oas) => {
if (err) {
console.error(err);
}
else {
// Initialize spectral object from Spectral API
const spectral = new Spectral();
// Register supported OpenAPUI formats
spectral.registerFormat("oas2", isOpenApiv2);
spectral.registerFormat("oas3", isOpenApiv3);
// Load default OpenAPI ruleset
spectral.loadRuleset(join(__dirname, './rulesets/.spectral.yaml'))
// Lint the provided 'oas' object
.then(() => spectral.run(oas))
.then(results => {
// Writes the lint results to a file
fs.writeFile('./output/cloudhome-results.json', JSON.stringify(results, null, 2), (err) => {
if (err) {
return console.error(err);
}
else {
console.log('OpenAPI description linted successfully at output/cloudhome-results.json!');
}
});
});
}
});
};
cloudhome();
apple();