Skip to content

Commit

Permalink
Allow passing schema as json object (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
zerberio committed Jun 16, 2015
1 parent 9cdecf7 commit fb45ae9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
21 changes: 10 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -51,6 +49,7 @@ exports.validate = function(raw) {
errors: errors
};
}
});
}

exports.validateUrl = function(dpurl, callback) {
Expand Down
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": ""
Expand Down

0 comments on commit fb45ae9

Please sign in to comment.