|
1 |
| -var Q = require('q'); |
2 |
| -var glob = require('glob'); |
3 |
| -var tv4 = require('tv4'); |
4 |
| -var ZSchema = require('z-schema'); |
5 | 1 | 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'); |
7 | 6 | 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'); |
9 | 10 |
|
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'); |
12 | 12 |
|
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'); |
67 | 19 | }
|
68 | 20 |
|
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); |
77 | 30 | }
|
78 | 31 |
|
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); |
82 | 37 |
|
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; |
88 | 41 |
|
89 |
| -var createJSONTest = function(file, validator) { |
90 |
| - if (validators.indexOf(validator) == -1) |
91 |
| - return; |
| 42 | + validate(swagger); |
92 | 43 |
|
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 | + }) |
96 | 53 | })
|
97 | 54 | }
|
98 | 55 |
|
99 | 56 | 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') |
118 | 58 | })
|
119 | 59 |
|
120 | 60 | 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') |
139 | 62 | })
|
0 commit comments