Skip to content

Commit

Permalink
Middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
jaw187 committed Jan 20, 2012
1 parent e6f1581 commit 00fd2a0
Showing 1 changed file with 36 additions and 8 deletions.
44 changes: 36 additions & 8 deletions lib/scrubr.js
Expand Up @@ -5,6 +5,8 @@
/////

var util=require('util'),
url=require('url'),
compr = require('compr'),
tests=require('./tests'),
scrubers=require('./scrubers'),
DEFINITION = null;
Expand All @@ -14,10 +16,9 @@ var validateDefinition = function (def) {
for (field in def) {
for (test in def[field]) {
if (test === "scrub") {
for (var i =0;i<def[field].scrub;i++) {
for (var i = 0; i < def[field].scrub; i++) {
if (!scrubers[def[field].scrub[i]]) {
badTests.push(s + " is not a known scruber");
console.log(s);
}
}
}
Expand All @@ -32,7 +33,16 @@ console.log(s);
else return true;
}

exports.scrub = function(data) {
exports.scrub = function() {
if (arguments.length > 1) {
var req = arguments[0],
res = arguments[1],
next = arguments[2],
routes = arguments[3],
data = req.body;
}
else data = arguments[0];

var failures = [],
field,
test,
Expand Down Expand Up @@ -89,20 +99,29 @@ exports.scrub = function(data) {
}
}
if (failures.length === 0) {
this.pass();
this.pass(next);
return true;
}
else {
this.fail(failures);
this.fail(failures,req,res,next,routes);
return false;
}
}
else {
this.fail(['DEFINITION does not exist']);
this.fail(['DEFINITION does not exist'],req,res,next,routes);
return false;
}
}

exports.middleware = function (routes) {
return function (req,res,next) {
if (req.body && !(compr.compare(req.body,{}))) {
exports.scrub(req,res,next,routes);
}
else next();
}
}

exports.define = function (def) {
defCheck = validateDefinition(def);
if (defCheck === true)
Expand All @@ -112,13 +131,22 @@ exports.define = function (def) {
}


exports.fail = function (failures) {
exports.fail = function (failures,req,res,next,routes) {
console.log("FAIL");
for (var i=0;i<failures.length;i++) {
console.log(failures[i]);
}

if (req && req.headers && req.headers.referer) {
req.url = url.parse(req.header('referer')).path;
req.method = 'GET';
req.scrubr = { failures: failures };
next();
}
else next('Referrer Undefined');
}

exports.pass = function () {
exports.pass = function (next) {
console.log("PASSSSSSS");
if (next) next();
}

0 comments on commit 00fd2a0

Please sign in to comment.