req-requires
Advanced tools
Comparing version 0.0.5 to 0.0.6
@@ -9,19 +9,39 @@ var requiredProperty = require('./requiredProperty'); | ||
//TODO: wrap this in a function that can receive a custom error handler | ||
module.exports.error = function(err, req, res, next){ | ||
if(err === 'req-requires error') | ||
res.send(400, req.requires.error); | ||
else | ||
next(err); | ||
if(err === 'req-requires error') | ||
module.exports.errorHandler(req.requires.error, req, res, next); | ||
else | ||
next(err); | ||
} | ||
module.exports.errorHandler = function(errorMessage, req, res, next){ | ||
res.send(400, errorMessage); | ||
} | ||
var requires = function(req, res, next){ | ||
this.property = function(name){ | ||
return new requiredProperty(req, name); | ||
var self = this; | ||
self.property = function(name){ | ||
return new requiredProperty(this, name); | ||
} | ||
this.fail = function(message){ | ||
this.error = message; | ||
self.assertFail = function(message){ | ||
self.error = message; | ||
throw('req-requires error'); | ||
} | ||
self.getProperty = function(propertyName){ | ||
var currentValue=req; | ||
var list=propertyName.split("."); | ||
for (var i=0; i<list.length; i++) | ||
{ | ||
currentValue=currentValue[list[i]]; | ||
if (typeof(currentValue)==='undefined') | ||
return currentValue; | ||
} | ||
return currentValue; | ||
} | ||
return self; | ||
} |
var requiredProperty = function(requiresContext, propertyName){ | ||
this.context = requiresContext; | ||
this.name = propertyName; | ||
this.value = getPropertyValue(this.context,this.name); | ||
this.value = this.context.getProperty(this.name); | ||
return this; | ||
@@ -10,18 +10,5 @@ } | ||
if(!assertion()) | ||
this.context.fail('Expected '+this.name+' '+assertionText); | ||
this.context.assertFail('Expected '+this.name+' '+assertionText); | ||
} | ||
function getPropertyValue(context, propertyName){ | ||
var currentValue=context; | ||
var list=propertyName.split("."); | ||
for (var i=0; i<list.length; i++) | ||
{ | ||
currentValue=currentValue[list[i]]; | ||
if (typeof(currentValue)==='undefined') | ||
return currentValue; | ||
} | ||
return currentValue; | ||
} | ||
module.exports = requiredProperty; |
{ | ||
"name": "req-requires", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"description": "Add assertions to ExpressJS middleware", | ||
@@ -5,0 +5,0 @@ "main": "./lib/req-requires.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4300
74