Skip to content

Commit 8b0e8e7

Browse files
committed
First iteration of simple validation
1 parent af58d66 commit 8b0e8e7

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
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

0 commit comments

Comments
 (0)