From e0753574354123a46aa2c8c49f07e002c839710e Mon Sep 17 00:00:00 2001 From: Trevor Livingston Date: Mon, 8 Sep 2014 13:21:27 -0500 Subject: [PATCH 1/2] added cache of schemas to allow override schemas by name --- lib/schema/index.js | 9 ++++++++- package.json | 2 +- test/fixtures/listing.json | 14 ++++++++++++++ test/test-schema.js | 8 ++++++++ 4 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 test/fixtures/listing.json diff --git a/lib/schema/index.js b/lib/schema/index.js index b46cdd6..0028b6f 100644 --- a/lib/schema/index.js +++ b/lib/schema/index.js @@ -3,10 +3,12 @@ var tv4 = require('tv4'), assert = require('assert'), fs = require('fs'), + thing = require('core-util-is'), path = require('path'); -var schemaPath, baseSchemaPath, baseSchema, modelSchema; +var cache, schemaPath, baseSchemaPath, baseSchema, modelSchema; +cache = {}; schemaPath = path.join(__dirname, 'swagger-spec/schemas/v1.2'); baseSchemaPath = path.join(schemaPath, 'apiDeclaration.json'); modelSchema = require(path.join(schemaPath, 'modelsObject')); @@ -20,6 +22,7 @@ fs.readdirSync(schemaPath).forEach(function (file) { var schema; schema = require(path.join(schemaPath, file)); + cache[file] = schema; tv4.addSchema(schema); }); @@ -34,6 +37,10 @@ module.exports = { validate: function validate(data, schema) { var results; + if (thing.isString(schema)) { + schema = cache[schema]; + } + results = tv4.validateResult(data, schema || baseSchema, true); return results; diff --git a/package.json b/package.json index ccbf9bd..bfb3a6f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "swaggerize-builder", - "version": "1.0.0-rc.1", + "version": "1.0.0-rc.2", "author": "Trevor Livingston ", "description": "Swagger based route building.", "main": "./lib/index", diff --git a/test/fixtures/listing.json b/test/fixtures/listing.json new file mode 100644 index 0000000..70a0115 --- /dev/null +++ b/test/fixtures/listing.json @@ -0,0 +1,14 @@ +{ + "swaggerVersion": "1.2", + "apiVersion": "v1", + "info": { + "title": "Greetings", + "description": "This is a resource listing document for greetings api." + }, + "apis": [ + { + "path": "/greetings", + "description": "operations on greetings." + } + ] +} diff --git a/test/test-schema.js b/test/test-schema.js index fe5598c..019840f 100644 --- a/test/test-schema.js +++ b/test/test-schema.js @@ -14,6 +14,14 @@ test('schema', function (t) { t.ok(results.valid, 'no errors'); }); + t.test('validate against cached schema', function (t) { + t.plan(1); + + var results = schema.validate(require('./fixtures/listing.json'), 'resourceListing.json'); + + t.ok(results.valid, 'no errors'); + }); + t.test('bad api', function (t) { t.plan(2); From 5a8363bcc6fc632cba246a648f0cf795910799fb Mon Sep 17 00:00:00 2001 From: Trevor Livingston Date: Mon, 8 Sep 2014 14:01:31 -0500 Subject: [PATCH 2/2] use requires cache in favor of making own --- lib/schema/index.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/schema/index.js b/lib/schema/index.js index 0028b6f..f50dc8f 100644 --- a/lib/schema/index.js +++ b/lib/schema/index.js @@ -6,9 +6,8 @@ var tv4 = require('tv4'), thing = require('core-util-is'), path = require('path'); -var cache, schemaPath, baseSchemaPath, baseSchema, modelSchema; +var schemaPath, baseSchemaPath, baseSchema, modelSchema; -cache = {}; schemaPath = path.join(__dirname, 'swagger-spec/schemas/v1.2'); baseSchemaPath = path.join(schemaPath, 'apiDeclaration.json'); modelSchema = require(path.join(schemaPath, 'modelsObject')); @@ -22,7 +21,6 @@ fs.readdirSync(schemaPath).forEach(function (file) { var schema; schema = require(path.join(schemaPath, file)); - cache[file] = schema; tv4.addSchema(schema); }); @@ -38,7 +36,7 @@ module.exports = { var results; if (thing.isString(schema)) { - schema = cache[schema]; + schema = require(path.join(schemaPath, schema)); } results = tv4.validateResult(data, schema || baseSchema, true);