Skip to content

Commit 3cb58cd

Browse files
committed
Merge pull request OAI#591 from APIs-guru/master
Clenup JS test. Resolve external $ref during tests.
2 parents 8e31017 + c976486 commit 3cb58cd

File tree

3 files changed

+54
-146
lines changed

3 files changed

+54
-146
lines changed

.travis.yml

+1-8
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,5 @@ scala:
44
- 2.10.4
55
script:
66
- npm install
7-
- gulp lint
7+
- npm test
88
- sbt ++$TRAVIS_SCALA_VERSION test
9-
- mocha
10-
env:
11-
- VALIDATORS=tv4
12-
- VALIDATORS=zschema
13-
matrix:
14-
allow_failures:
15-
- env: VALIDATORS=tv4

package.json

+10-18
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,24 @@
11
{
2-
"name": "swagger-validator",
3-
"version": "0.0.1",
4-
"description": "validates a file",
5-
"author": {
6-
"name": "Tony Tam",
7-
"email": "[email protected]",
8-
"url": "http://swagger.io"
9-
},
10-
"license": "Apache",
11-
"readmeFilename": "README.md",
12-
"dependencies": {
13-
"tv4": "~1.1.x",
14-
"chai": "1.9.x"
2+
"private": true,
3+
"scripts": {
4+
"test": "gulp lint && mocha"
155
},
6+
"dependencies": {},
167
"devDependencies": {
17-
"glob": "^4.0.5",
8+
"chai": "^3.5.0",
9+
"glob": "^7.0.0",
1810
"gulp": "~3.8.x",
1911
"gulp-ext-replace": "^0.1.0",
2012
"gulp-jsonlint": "0.0.3",
2113
"gulp-util": "^3.0.0",
2214
"gulp-yaml": "0.0.3",
2315
"js-yaml": "^3.1.0",
16+
"json-schema-ref-parser": "^2.2.0",
2417
"json2yaml": "^1.0.3",
2518
"jsonschema": "^1.0.0",
19+
"lodash": "^4.5.1",
2620
"map-stream": "^0.1.0",
27-
"mocha": "^1.21.3",
28-
"q": "^1.0.1",
29-
"request": "^2.39.0",
30-
"z-schema": "^2.4.9"
21+
"mocha": "^2.4.5",
22+
"z-schema": "^3.16.1"
3123
}
3224
}

test/validate.js

+43-120
Original file line numberDiff line numberDiff line change
@@ -1,139 +1,62 @@
1-
var Q = require('q');
2-
var glob = require('glob');
3-
var tv4 = require('tv4');
4-
var ZSchema = require('z-schema');
51
var fs = require('fs');
6-
var assert = require('chai').assert;
2+
var path = require('path');
3+
4+
var _ = require('lodash');
5+
var glob = require('glob');
76
var yaml = require('js-yaml');
8-
var request = require("request")
7+
var ZSchema = require('z-schema');
8+
var expect = require('chai').expect;
9+
var RefParser = require('json-schema-ref-parser');
910

10-
var schema = JSON.parse(fs.readFileSync("./schemas/v2.0/schema.json", 'utf8'));
11-
var validators = (process.env.VALIDATORS || "tv4,zschema").split(",")
11+
var schema = require('../schemas/v2.0/schema.json');
1212

13-
var validationMethods = {
14-
tv4: {
15-
setup: function() {
16-
var deferred = Q.defer();
17-
request({
18-
url: "http://json-schema.org/draft-04/schema",
19-
json: true
20-
}, function (error, response, body) {
21-
if (!error && response.statusCode === 200) {
22-
tv4.addSchema("http://json-schema.org/draft-04/schema", body);
23-
deferred.resolve();
24-
} else {
25-
deferred.reject(new Error("Request failed"));
26-
}
27-
})
28-
return deferred.promise;
29-
},
30-
validate: function(schema, data) {
31-
var result = tv4.validateMultiple(data, schema, true, true);
32-
assert(result.missing.length == 0, "Missing schemas: " + result.missing)
33-
if (result.errors.length > 0) {
34-
for (i in result.errors) {
35-
// Remove stack trace so results are readable
36-
delete result.errors[i].stack;
37-
}
38-
}
39-
assert(result.valid == true, "Validation failed: " + JSON.stringify(result, null, "\t"));
40-
},
41-
},
42-
zschema: {
43-
setup: function() {
44-
var deferred = Q.defer()
45-
request({
46-
url: "http://json-schema.org/draft-04/schema"
47-
}, function (error, response, body) {
48-
if (!error && response.statusCode === 200) {
49-
ZSchema.setRemoteReference("http://json-schema.org/draft-04/schema", body);
50-
deferred.resolve();
51-
} else {
52-
deferred.reject(new Error("Request failed"));
53-
}
54-
})
55-
return deferred.promise;
56-
},
57-
validate: function(schema, data) {
58-
var validator = new ZSchema({ sync: true });
59-
var valid = validator.validate(data, schema);
60-
if (!valid) {
61-
var error = validator.getLastError();
62-
throw new Error("ZSchema failed: " + JSON.stringify(error, null, "\t"));
63-
}
64-
assert(valid == true)
65-
}
66-
}
13+
function validate(data) {
14+
var validator = new ZSchema();
15+
validator.validate(data, schema);
16+
var error = validator.getLastError();
17+
error = JSON.stringify(error, null, 2);
18+
expect(error).to.deep.equal('null');
6719
}
6820

69-
var setupValidators = function(done) {
70-
var setupPromises = []
71-
validators.forEach(function(validator) {
72-
setupPromises.push(validationMethods[validator].setup())
73-
});
74-
return Q.all(setupPromises).then(function() {
75-
done();
76-
})
21+
function readFile(file, isYaml) {
22+
var ext = path.extname(file);
23+
var data = fs.readFileSync(file, 'utf8');
24+
25+
expect(ext).to.be.oneOf(['.json', '.yaml']);
26+
if (ext === '.yaml')
27+
return yaml.safeLoad(data);
28+
else if (ext === '.json')
29+
return JSON.parse(data);
7730
}
7831

79-
var createYAMLTest = function(file, validator) {
80-
if (validators.indexOf(validator) == -1)
81-
return;
32+
function validateFiles(pattern) {
33+
files = glob.sync(pattern)
34+
files.forEach(function(file) {
35+
it("should validate " + file, function() {
36+
var swagger = readFile(file);
8237

83-
it("should validate " + file + " with " + validator, function() {
84-
var data = yaml.safeLoad(fs.readFileSync(file, 'utf8'));
85-
validationMethods[validator].validate(schema, data);
86-
})
87-
}
38+
expect(swagger).to.be.an('object');
39+
if (_.isUndefined(swagger.swagger))
40+
return;
8841

89-
var createJSONTest = function(file, validator) {
90-
if (validators.indexOf(validator) == -1)
91-
return;
42+
validate(swagger);
9243

93-
it("should validate " + file + " with " + validator, function() {
94-
var data = JSON.parse(fs.readFileSync(file, 'utf8'));
95-
validationMethods[validator].validate(schema, data);
44+
return RefParser.dereference(file, {
45+
$refs: {
46+
internal: false // Don't dereference internal $refs, only external
47+
}
48+
})
49+
.then(function(resolveSwagger) {
50+
validate(resolveSwagger);
51+
});
52+
})
9653
})
9754
}
9855

9956
describe('JSON Samples', function() {
100-
before(function(done) {
101-
setupValidators(done);
102-
})
103-
exclusions = ["./examples/v2.0/json/petstore-separate/common/Error.json",
104-
"./examples/v2.0/json/petstore-separate/spec/NewPet.json",
105-
"./examples/v2.0/json/petstore-separate/spec/Pet.json",
106-
"./examples/v2.0/json/petstore-separate/spec/parameters.json"]
107-
108-
files = glob.sync("./examples/**/*.json")
109-
validators.forEach(function(validator) {
110-
files.forEach(function(file) {
111-
if (exclusions.indexOf(file) == -1) {
112-
createJSONTest(file, validator);
113-
} else {
114-
//TODO: validate separate schema files in exclusion list
115-
}
116-
})
117-
})
57+
validateFiles('./examples/**/*.json')
11858
})
11959

12060
describe('YAML Samples', function() {
121-
before(function(done) {
122-
setupValidators(done);
123-
})
124-
exclusions = ["./examples/v2.0/yaml/petstore-separate/common/Error.yaml",
125-
"./examples/v2.0/yaml/petstore-separate/spec/NewPet.yaml",
126-
"./examples/v2.0/yaml/petstore-separate/spec/Pet.yaml",
127-
"./examples/v2.0/yaml/petstore-separate/spec/parameters.yaml"]
128-
129-
files = glob.sync("./examples/**/*.yaml")
130-
validators.forEach(function(validator) {
131-
files.forEach(function(file) {
132-
if (exclusions.indexOf(file) == -1) {
133-
createYAMLTest(file, validator);
134-
} else {
135-
//TODO: validate separate schema files in exclusion list
136-
}
137-
})
138-
})
61+
validateFiles('./examples/**/*.yaml')
13962
})

0 commit comments

Comments
 (0)