From c9764861e5e00322eba359279ed77f50444a829c Mon Sep 17 00:00:00 2001 From: Ivan Goncharov Date: Wed, 2 Mar 2016 16:17:56 +0200 Subject: [PATCH] Clenup JS test. Resolve external $ref during tests. --- .travis.yml | 9 +-- package.json | 28 +++----- test/validate.js | 163 +++++++++++++---------------------------------- 3 files changed, 54 insertions(+), 146 deletions(-) diff --git a/.travis.yml b/.travis.yml index 4aff41d513..ceb7fef10c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,12 +4,5 @@ scala: - 2.10.4 script: - npm install - - gulp lint + - npm test - sbt ++$TRAVIS_SCALA_VERSION test - - mocha -env: - - VALIDATORS=tv4 - - VALIDATORS=zschema -matrix: - allow_failures: - - env: VALIDATORS=tv4 diff --git a/package.json b/package.json index 4fa476b77a..b182b8483b 100644 --- a/package.json +++ b/package.json @@ -1,32 +1,24 @@ { - "name": "swagger-validator", - "version": "0.0.1", - "description": "validates a file", - "author": { - "name": "Tony Tam", - "email": "fehguy@gmail.com", - "url": "http://swagger.io" - }, - "license": "Apache", - "readmeFilename": "README.md", - "dependencies": { - "tv4": "~1.1.x", - "chai": "1.9.x" + "private": true, + "scripts": { + "test": "gulp lint && mocha" }, + "dependencies": {}, "devDependencies": { - "glob": "^4.0.5", + "chai": "^3.5.0", + "glob": "^7.0.0", "gulp": "~3.8.x", "gulp-ext-replace": "^0.1.0", "gulp-jsonlint": "0.0.3", "gulp-util": "^3.0.0", "gulp-yaml": "0.0.3", "js-yaml": "^3.1.0", + "json-schema-ref-parser": "^2.2.0", "json2yaml": "^1.0.3", "jsonschema": "^1.0.0", + "lodash": "^4.5.1", "map-stream": "^0.1.0", - "mocha": "^1.21.3", - "q": "^1.0.1", - "request": "^2.39.0", - "z-schema": "^2.4.9" + "mocha": "^2.4.5", + "z-schema": "^3.16.1" } } diff --git a/test/validate.js b/test/validate.js index 243442d3e0..849f68a176 100644 --- a/test/validate.js +++ b/test/validate.js @@ -1,139 +1,62 @@ -var Q = require('q'); -var glob = require('glob'); -var tv4 = require('tv4'); -var ZSchema = require('z-schema'); var fs = require('fs'); -var assert = require('chai').assert; +var path = require('path'); + +var _ = require('lodash'); +var glob = require('glob'); var yaml = require('js-yaml'); -var request = require("request") +var ZSchema = require('z-schema'); +var expect = require('chai').expect; +var RefParser = require('json-schema-ref-parser'); -var schema = JSON.parse(fs.readFileSync("./schemas/v2.0/schema.json", 'utf8')); -var validators = (process.env.VALIDATORS || "tv4,zschema").split(",") +var schema = require('../schemas/v2.0/schema.json'); -var validationMethods = { - tv4: { - setup: function() { - var deferred = Q.defer(); - request({ - url: "http://json-schema.org/draft-04/schema", - json: true - }, function (error, response, body) { - if (!error && response.statusCode === 200) { - tv4.addSchema("http://json-schema.org/draft-04/schema", body); - deferred.resolve(); - } else { - deferred.reject(new Error("Request failed")); - } - }) - return deferred.promise; - }, - validate: function(schema, data) { - var result = tv4.validateMultiple(data, schema, true, true); - assert(result.missing.length == 0, "Missing schemas: " + result.missing) - if (result.errors.length > 0) { - for (i in result.errors) { - // Remove stack trace so results are readable - delete result.errors[i].stack; - } - } - assert(result.valid == true, "Validation failed: " + JSON.stringify(result, null, "\t")); - }, - }, - zschema: { - setup: function() { - var deferred = Q.defer() - request({ - url: "http://json-schema.org/draft-04/schema" - }, function (error, response, body) { - if (!error && response.statusCode === 200) { - ZSchema.setRemoteReference("http://json-schema.org/draft-04/schema", body); - deferred.resolve(); - } else { - deferred.reject(new Error("Request failed")); - } - }) - return deferred.promise; - }, - validate: function(schema, data) { - var validator = new ZSchema({ sync: true }); - var valid = validator.validate(data, schema); - if (!valid) { - var error = validator.getLastError(); - throw new Error("ZSchema failed: " + JSON.stringify(error, null, "\t")); - } - assert(valid == true) - } - } +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'); } -var setupValidators = function(done) { - var setupPromises = [] - validators.forEach(function(validator) { - setupPromises.push(validationMethods[validator].setup()) - }); - return Q.all(setupPromises).then(function() { - done(); - }) +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); } -var createYAMLTest = function(file, validator) { - if (validators.indexOf(validator) == -1) - return; +function validateFiles(pattern) { + files = glob.sync(pattern) + files.forEach(function(file) { + it("should validate " + file, function() { + var swagger = readFile(file); - it("should validate " + file + " with " + validator, function() { - var data = yaml.safeLoad(fs.readFileSync(file, 'utf8')); - validationMethods[validator].validate(schema, data); - }) -} + expect(swagger).to.be.an('object'); + if (_.isUndefined(swagger.swagger)) + return; -var createJSONTest = function(file, validator) { - if (validators.indexOf(validator) == -1) - return; + validate(swagger); - it("should validate " + file + " with " + validator, function() { - var data = JSON.parse(fs.readFileSync(file, 'utf8')); - validationMethods[validator].validate(schema, data); + return RefParser.dereference(file, { + $refs: { + internal: false // Don't dereference internal $refs, only external + } + }) + .then(function(resolveSwagger) { + validate(resolveSwagger); + }); + }) }) } describe('JSON Samples', function() { - before(function(done) { - setupValidators(done); - }) - exclusions = ["./examples/v2.0/json/petstore-separate/common/Error.json", - "./examples/v2.0/json/petstore-separate/spec/NewPet.json", - "./examples/v2.0/json/petstore-separate/spec/Pet.json", - "./examples/v2.0/json/petstore-separate/spec/parameters.json"] - - files = glob.sync("./examples/**/*.json") - validators.forEach(function(validator) { - files.forEach(function(file) { - if (exclusions.indexOf(file) == -1) { - createJSONTest(file, validator); - } else { - //TODO: validate separate schema files in exclusion list - } - }) - }) + validateFiles('./examples/**/*.json') }) describe('YAML Samples', function() { - before(function(done) { - setupValidators(done); - }) - exclusions = ["./examples/v2.0/yaml/petstore-separate/common/Error.yaml", - "./examples/v2.0/yaml/petstore-separate/spec/NewPet.yaml", - "./examples/v2.0/yaml/petstore-separate/spec/Pet.yaml", - "./examples/v2.0/yaml/petstore-separate/spec/parameters.yaml"] - - files = glob.sync("./examples/**/*.yaml") - validators.forEach(function(validator) { - files.forEach(function(file) { - if (exclusions.indexOf(file) == -1) { - createYAMLTest(file, validator); - } else { - //TODO: validate separate schema files in exclusion list - } - }) - }) + validateFiles('./examples/**/*.yaml') })