Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to configure validation middleware. #53

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions jive-sdk-service/lib/baseSetup.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,12 @@ exports.setupRoutes = function(app, definitionName, routesPath, prefix) {

httpVerb = candidate['verb'] || 'get'; // default to GET verb
httpVerb = httpVerb.toLowerCase();
app[httpVerb](routeContextPath, candidate['route']);

if (candidate.validate) {
app[httpVerb](routeContextPath, candidate.validate, candidate['route']);
} else {
app[httpVerb](routeContextPath, candidate['route']);
}

// lock the route if its marked to be locked
if ( candidate['jiveLocked'] ) {
Expand Down Expand Up @@ -280,4 +285,4 @@ exports.setupServices = function( app, definitionName, svcDir, setupEventListene
return exports.legalServiceFileExtensions.indexOf(path.extname( currentFsItem ) ) > -1;
}
);
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
exports.absolute = {
'verb' : 'get',
'validate' : function (req, res, next) {
if (req.params.id === 1) {
return next();
}
res.status(400).end('invalid');
},
'path' : '/validated/:id',
'route' : function(req, res) {
res.end('locked');
}
};
27 changes: 27 additions & 0 deletions test/unit/setup/testcases/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,33 @@ describe('jive', function () {
})
});

it('add validation middleware', function (done) {
var jive = this['jive'];
var testUtils = this['testUtils'];

var options = testUtils.createBaseServiceOptions('/services/tile_routes');
delete options['role'];
options['port'] = 5555; options['logLevel'] = 'FATAL'; options['clientUrl'] = 'http://localhost:5555';
testUtils.setupService(jive, options).then( function(service) {
jive.util.buildRequest('http://localhost:5555/validated/1').then( function(r) {
assert.ok(r['entity']);
return jive.util.buildRequest('http://localhost:5555/validated/unexpected-value');
}).then(
function(r) {
assert.fail(r, 'expected error');
},

function(e) {
assert.ok(e);
assert.equal(e['statusCode'], 400);
}
).then( function() {
service.stop().then( function() {
done();
});
});
});
});
});

});
Expand Down