forked from OAI/OpenAPI-Specification
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate.js
62 lines (51 loc) · 1.5 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
var fs = require('fs');
var path = require('path');
var _ = require('lodash');
var glob = require('glob');
var yaml = require('js-yaml');
var ZSchema = require('z-schema');
var expect = require('chai').expect;
var RefParser = require('json-schema-ref-parser');
var schema = require('../schemas/v2.0/schema.json');
function validate(data) {
var validator = new ZSchema();
validator.validate(data, schema);
var error = validator.getLastError();
error = JSON.stringify(error, null, 2);
expect(error).to.deep.equal('null');
}
function readFile(file, isYaml) {
var ext = path.extname(file);
var data = fs.readFileSync(file, 'utf8');
expect(ext).to.be.oneOf(['.json', '.yaml']);
if (ext === '.yaml')
return yaml.safeLoad(data);
else if (ext === '.json')
return JSON.parse(data);
}
function validateFiles(pattern) {
files = glob.sync(pattern)
files.forEach(function(file) {
it("should validate " + file, function() {
var swagger = readFile(file);
expect(swagger).to.be.an('object');
if (_.isUndefined(swagger.swagger))
return;
validate(swagger);
return RefParser.dereference(file, {
$refs: {
internal: false // Don't dereference internal $refs, only external
}
})
.then(function(resolveSwagger) {
validate(resolveSwagger);
});
})
})
}
describe('JSON Samples', function() {
validateFiles('./examples/**/*.json')
})
describe('YAML Samples', function() {
validateFiles('./examples/**/*.yaml')
})