We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent af58d66 commit 8b0e8e7Copy full SHA for 8b0e8e7
lib/handlers/validate.js
@@ -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
@@ -10,6 +10,7 @@ const del = require('./handlers/delete')
const patch = require('./handlers/patch')
const index = require('./handlers/index')
const copy = require('./handlers/copy')
+const validate = require('./handlers/validate')
function LdpMiddleware (corsSettings) {
const router = express.Router('/')
@@ -23,9 +24,9 @@ function LdpMiddleware (corsSettings) {
router.copy('/*', allow('Write'), copy)
router.get('/*', index, allow('Read'), header.addPermissions, get)
- router.post('/*', allow('Append'), post)
- router.patch('/*', allow('Append'), patch)
- router.put('/*', allow('Write'), put)
+ router.post('/*', allow('Append'), validate, post)
+ router.patch('/*', allow('Append'), validate, patch)
29
+ router.put('/*', allow('Write'), validate, put)
30
router.delete('/*', allow('Write'), del)
31
32
return router
0 commit comments