/
index.js
89 lines (75 loc) · 2.94 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
89
import YAML from 'js-yaml';
import JSONSchemaValidator from 'src/plugins/json-schema-validator/validator/index.js';
import fs from 'fs';
import path from 'path';
const swagger2SchemaYaml = fs.readFileSync(path.join(__dirname, '../../../../src/plugins/json-schema-validator/swagger2-schema.yaml')).toString();
const oas3SchemaYaml = fs.readFileSync(path.join(__dirname, '../../../../src/plugins/json-schema-validator/oas3-schema.yaml')).toString();
const swagger2Schema = YAML.load(swagger2SchemaYaml);
const oas3Schema = YAML.load(oas3SchemaYaml);
var testDocuments = fs
.readdirSync(__dirname + '/test-documents')
.filter(path => path.endsWith('yaml'))
.map(path => ({
path: '/test-documents/' + path,
contentString: fs.readFileSync(__dirname + '/test-documents/' + path, 'utf8'),
}))
.map(doc => ({
path: doc.path,
content: YAML.load(doc.contentString)
}));
testDocuments.forEach(doc => {
const { path, content } = doc;
const { meta = {}, cases = [] } = content;
const validator = new JSONSchemaValidator();
validator.addSchema(swagger2Schema, ['openapi-2.0']);
validator.addSchema(oas3Schema, ['openapi-3.0']);
const rootDescribe = meta.skip ? describe.skip : describe;
rootDescribe('schema validation plugin - ' + (meta.title || path), function() {
if(content.input && content.output) {
// fold simple input/output docs into implicit cases
cases.push({
input: content.input,
output: content.output,
});
}
if(cases && cases.length) {
cases.forEach(currentCase => {
const versionDefaultSchema = currentCase.input.openapi && !currentCase.input.swagger ? 'openapi-3.0' : 'openapi-2.0';
const result = validator.validate({
jsSpec: currentCase.input,
specStr: '', // not needed here
schemaPath: versionDefaultSchema,
source: 'structural',
});
if(currentCase.name) {
// only create a new describe block if we have a name
describe(currentCase.name || '', () => {
assertCaseExpectations(currentCase, result);
});
} else {
// else, just do the assertions under the root describe block
assertCaseExpectations(currentCase, result);
}
});
}
});
});
function assertCaseExpectations(currentCase, result) {
const itFn = currentCase.skip ? it.skip : it;
if (currentCase.output.match !== undefined) {
itFn('should match expected error output', function () {
expect(result.toString()).toMatch(currentCase.output.match.toString());
});
}
if (currentCase.output.length !== undefined) {
itFn('should have expected array length', function () {
expect(result).toBeInstanceOf(Array);
expect(result.length).toBe(currentCase.output.length);
});
}
if(currentCase.output.equal !== undefined) {
itFn('should equal expected value', function() {
expect(result).toEqual(currentCase.output.equal);
});
}
}