From 924cfce2c27a0a164f368e07e3c9b7148cd176bf Mon Sep 17 00:00:00 2001 From: Rufus Pollock Date: Sun, 17 Aug 2014 21:57:14 +0100 Subject: [PATCH] [fixes #124,validate][m]: upgrade to use datapackage-validate library. * This also fixes #29 - validating json table schema in datapackage.json * Also fixes #59 - informative JSON errors * Also added in: some (very simple) API docs that doc the validate API --- doc/api.md | 14 ++++++++++++++ package.json | 2 ++ routes/index.js | 15 ++++++++------- views/tools/dp/validate.html | 15 ++++++++++++++- 4 files changed, 38 insertions(+), 8 deletions(-) create mode 100644 doc/api.md diff --git a/doc/api.md b/doc/api.md new file mode 100644 index 000000000..feb53c767 --- /dev/null +++ b/doc/api.md @@ -0,0 +1,14 @@ +# API + +## Create DataPackage.json + +``` +GET /tools/create.json?url=... +``` + +## Validate DataPackage.json + +``` +GET /tools/validate.json?url={path-to-data-package} +``` + diff --git a/package.json b/package.json index 5e726a193..7b2185140 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,8 @@ "csv": "", "underscore": "", "datapackage": "0.3.0", + "datapackage-spec": "git://github.com/okfn/datapackage-spec", + "datapackage-validate": ">=0.2.2", "datapackage-read": "" } , "devDependencies": { diff --git a/routes/index.js b/routes/index.js index 508851e3e..466876738 100644 --- a/routes/index.js +++ b/routes/index.js @@ -7,6 +7,8 @@ var fs = require('fs') , config = require('../lib/config') , tools = require('datapackage') , model = require('../lib/model.js') + , spec = require('datapackage-spec') + , validate = require('datapackage-validate') ; var catalog = new model.Catalog(); @@ -171,23 +173,22 @@ exports.toolsDpCreate = function(req, res) { exports.toolsDpValidateJSON = function(req, res) { // handle base urls as well as full urls - var dpurl = req.query.url.replace(/datapackage.json$/, ''); - var dpurl = dpurl.replace(/\/$/, ''); - dpurl += '/datapackage.json'; - tools.validateUrl(dpurl, function(data) { + var dpurl = spec.parse(req.query.url).dataPackageJsonUrl; + validate.validateUrl(dpurl, function(data) { res.json(data); }); }; exports.toolsDpValidate = function(req, res) { - var url = req.query.url; + var url = spec.parse(req.query.url).dataPackageJsonUrl; if (!url) { res.render('tools/dp/validate.html'); } else { - tools.validateUrl(url, function(data) { + validate.validateUrl(url, function(data) { res.render('tools/dp/validate.html', { url: url, - result: data + result: data, + errorsAsJson: JSON.stringify(data.errors, null, 2) }); }); } diff --git a/views/tools/dp/validate.html b/views/tools/dp/validate.html index 3942344ea..7475b6a64 100644 --- a/views/tools/dp/validate.html +++ b/views/tools/dp/validate.html @@ -10,6 +10,9 @@

Data Package Validator

+

+ API also available +


@@ -33,9 +36,19 @@

    {% for error in result.errors %} -
  • {{error.message}}
  • +
  • + [{{error.type.toUpperCase()}}] {{error.message}} + {% if error.type == 'json' %} + (line: {{error.line}}, character: {{error.character}}) + {% endif %} +
  • {% endfor %}
+ +
+{{errorsAsJson}}
+
+ {% endif %} {% endblock %}