mirror of
https://github.com/zhigang1992/deployd.git
synced 2026-06-06 06:19:59 +08:00
35 lines
756 B
JavaScript
35 lines
756 B
JavaScript
/**
|
|
* Dependencies
|
|
*/
|
|
|
|
var revalidator = require('revalidator');
|
|
|
|
/**
|
|
* Validate the attached resource and request.
|
|
*/
|
|
|
|
module.exports = function (req, res, next) {
|
|
var method = req.method
|
|
, resource = req.resource
|
|
, validation
|
|
, err
|
|
;
|
|
|
|
// root should skip validation
|
|
if(req.isRoot) return next();
|
|
|
|
// skip without a resource
|
|
if(!resource) return next({error: 'Could not find a resource to validate.', status: 404});
|
|
|
|
// 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);
|
|
}
|
|
|
|
} |