Skip to content

Commit 48da61c

Browse files
committed
First iteration of simple validation
1 parent 0e3392f commit 48da61c

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

lib/handlers/validate.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module.exports = handler
2+
3+
const bodyParser = require('body-parser')
4+
const error = require('../http-error')
5+
const LDP = require('../ldp')
6+
const $rdf = require('rdflib')
7+
const debug = require('../debug')
8+
9+
function handler (req, res, next) {
10+
bodyParser.text({ type: () => true })(req, res, () => validate(req, res, next))
11+
}
12+
13+
function validate (req, res, next) {
14+
const contentType = req.get('content-type')
15+
if (!LDP.mimetypeIsRdf(contentType)) {
16+
return next()
17+
}
18+
19+
const resourceGraph = $rdf.graph()
20+
const requestUri = `${req.protocol}//${req.get('host')}${req.originalUrl}`
21+
try {
22+
$rdf.parse(req.body, resourceGraph, requestUri, contentType)
23+
} catch (err) {
24+
debug.handlers('VALIDATE -- Error parsing data: ' + err)
25+
return next(error(400, 'Unable to parse the body of request'))
26+
}
27+
next()
28+
}

lib/ldp-middleware.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const del = require('./handlers/delete')
1010
const patch = require('./handlers/patch')
1111
const index = require('./handlers/index')
1212
const copy = require('./handlers/copy')
13+
const validate = require('./handlers/validate')
1314

1415
function LdpMiddleware (corsSettings) {
1516
const router = express.Router('/')
@@ -23,9 +24,9 @@ function LdpMiddleware (corsSettings) {
2324

2425
router.copy('/*', allow('Write'), copy)
2526
router.get('/*', index, allow('Read'), header.addPermissions, get)
26-
router.post('/*', allow('Append'), post)
27-
router.patch('/*', allow('Append'), patch)
28-
router.put('/*', allow('Write'), put)
27+
router.post('/*', allow('Append'), validate, post)
28+
router.patch('/*', allow('Append'), validate, patch)
29+
router.put('/*', allow('Write'), validate, put)
2930
router.delete('/*', allow('Write'), del)
3031

3132
return router

lib/ldp.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ class LDP {
463463
return ensureNotExists(this, path.join(containerURI, filename))
464464
}
465465

466-
mimetypeIsRdf (mimeType) {
466+
static mimetypeIsRdf (mimeType) {
467467
return RDF_MIME_TYPES.indexOf(mimeType) !== -1
468468
}
469469
}

0 commit comments

Comments
 (0)