mirror of
https://github.com/zhigang1992/deployd.git
synced 2026-06-05 20:02:13 +08:00
51 lines
1.3 KiB
JavaScript
51 lines
1.3 KiB
JavaScript
/**
|
|
* Dependencies
|
|
*/
|
|
|
|
var revalidator = require('revalidator')
|
|
, isIdentifier = require('./storage').isIdentifier
|
|
;
|
|
|
|
/**
|
|
* Validate the attached resource and request.
|
|
*/
|
|
|
|
module.exports = function (req, res, next) {
|
|
var method = req.method
|
|
, resource = req.resource
|
|
, validation
|
|
, err
|
|
;
|
|
|
|
// rewrite queries from references
|
|
if(req.references && req.references.length && isIdentifier(req.references[0])) {
|
|
// if the first reference is an id
|
|
// rewrite the query to pull it
|
|
req.query._id = req.references[0];
|
|
req.one = true;
|
|
}
|
|
|
|
// root (or local) should skip validation
|
|
if(req.isRoot || !req.isRemote) return next();
|
|
|
|
// skip without a resource
|
|
if(!resource) return next({error: 'Could not find a resource to validate.', status: 404});
|
|
|
|
// if modifying data, require an id
|
|
if((req.method === 'PUT' || req.method === 'DELETE') && (!req.query || !req.query._id)) {
|
|
return next({error: 'An _id must be included when modifying a resource.'});
|
|
}
|
|
|
|
// if trying to write data
|
|
if((method === 'POST' || method === 'PUT') && resource && resource.properties) {
|
|
|
|
// validate JSON
|
|
validation = revalidator.validate(req.body, resource);
|
|
err = validation.valid ? err : validation;
|
|
next(err);
|
|
} else {
|
|
// continue
|
|
next(err);
|
|
}
|
|
|
|
} |