From fb45ae974afc0fe9ebed7e9930bff8f31c8b48ee Mon Sep 17 00:00:00 2001 From: nide Date: Tue, 16 Jun 2015 12:58:31 +0300 Subject: [PATCH] Allow passing schema as json object (#6) --- index.js | 21 ++++++++++----------- package.json | 8 +++++--- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/index.js b/index.js index 1decb8a..cb0ed47 100644 --- a/index.js +++ b/index.js @@ -4,17 +4,11 @@ var url = require('url') , path = require('path') , tv4 = require('tv4') , request = require('request') + , Promise = require('promise-polyfill') + , _ = require('underscore') ; -var schemasPath = path.join(__dirname, 'schemas'); - -var schemas = { - 'dataPackage': JSON.parse(fs.readFileSync( - path.join(schemasPath, 'data-package.json') - )) -}; - -exports.validate = function(raw) { +exports.validate = function(raw, schema) { var json = raw; if (typeof(json) == 'string') { var lint = jsonlint(json); @@ -34,8 +28,12 @@ exports.validate = function(raw) { json = JSON.parse(raw); } - var report = tv4.validateMultiple(json, schemas.dataPackage); - var errors = report.errors.map(function(error) { + // For consistency reasons always return Promise + return new Promise(function(RS, RJ) { + if(_.isObject(schema) && !_.isArray(schema) && !_.isFunction(schema)) + RS(tv4.validateMultiple(json, schema)); + }).then(function(R) { + var errors = R.errors.map(function(error) { delete error.stack; error.type = 'schema'; return error; @@ -51,6 +49,7 @@ exports.validate = function(raw) { errors: errors }; } + }); } exports.validateUrl = function(dpurl, callback) { diff --git a/package.json b/package.json index 64bcfea..94d11ab 100644 --- a/package.json +++ b/package.json @@ -22,9 +22,11 @@ ], "main": "./index", "dependencies": { - "tv4": "" - , "json-lint": "" - , "request": "" + "json-lint": "", + "promise-polyfill": "^2.0.2", + "request": "", + "tv4": "", + "underscore": "^1.8.3" }, "devDependencies": { "mocha": ""