Skip to content

Commit

Permalink
[fixes #124,validate][m]: upgrade to use datapackage-validate library.
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
rufuspollock committed Aug 17, 2014
1 parent 7d836ae commit 924cfce
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
14 changes: 14 additions & 0 deletions doc/api.md
Original file line number Diff line number Diff line change
@@ -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}
```

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
15 changes: 8 additions & 7 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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)
});
});
}
Expand Down
15 changes: 14 additions & 1 deletion views/tools/dp/validate.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ <h1>Data Package Validator</h1>
</div>

<form class="form js-info" action="">
<p style="float: right;">
<small><a href="/doc/api">API also available</a></small>
</p>
<input type="text" name="url" value="{{url}}" placeholder="Paste link to your datapackage.json here" class="span6" />
<br />
<button class="btn">Validate</button>
Expand All @@ -33,9 +36,19 @@ <h2>

<ul>
{% for error in result.errors %}
<li>{{error.message}}</li>
<li>
[{{error.type.toUpperCase()}}] {{error.message}}
{% if error.type == 'json' %}
(line: {{error.line}}, character: {{error.character}})
{% endif %}
</li>
{% endfor %}
</ul>

<pre>
{{errorsAsJson}}
</pre>

{% endif %}
</div>
{% endblock %}
Expand Down

0 comments on commit 924cfce

Please sign in to comment.