Skip to content

Commit

Permalink
response validation modify. Closes #1723
Browse files Browse the repository at this point in the history
  • Loading branch information
Eran Hammer committed Nov 5, 2014
1 parent 5074dc2 commit f75a5fe
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/Reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,7 @@ The following options are available when adding a route:
- `failAction` - defines what to do when a response fails validation. Options are:
- `error` - return an Internal Server Error (500) error response. This is the default value.
- `log` - log the error but send the response.
- `modify` - if `true`, applies the validation rule changes to the response. Defaults to `false`.

- `cache` - if the route method is 'GET', the route can be configured to include caching directives in the response using the following options:
- `privacy` - determines the privacy flag included in client-side caching using the 'Cache-Control' header. Values are:
Expand Down
6 changes: 4 additions & 2 deletions lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,10 @@ internals.routeConfig = Joi.object({
response: Joi.object({
schema: Joi.alternatives(Joi.object(), Joi.func()).allow(true, false).required(),
sample: Joi.number().min(0).max(100),
failAction: Joi.string().valid('error', 'log')
}),
failAction: Joi.string().valid('error', 'log'),
modify: Joi.boolean()
})
.without('modify', 'sample'),
cache: Joi.object({
privacy: Joi.string().valid('default', 'public', 'private'),
expiresIn: Joi.number(),
Expand Down
6 changes: 6 additions & 0 deletions lib/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ exports.response = function (request, next) {
var postValidate = function (err, value) {

if (!err) {
if (value !== undefined &&
request.route.response.modify) {

request.response.source = value;
}

return next();
}

Expand Down
60 changes: 60 additions & 0 deletions test/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,66 @@ describe('Validation', function () {
});
});

it('validates and modifies response', function (done) {

var handler = function (request, reply) {

return reply({ a: 1, b: 2 });
};

var server = new Hapi.Server({ debug: false });
server.connection();
server.route({
method: 'GET',
path: '/',
config: {
response: {
schema: Joi.object({
a: Joi.number()
}).options({ stripUnknown: true }),
modify: true
}
},
handler: handler
});

server.inject('/', function (res) {

expect(res.statusCode).to.equal(200);
expect(res.result).to.deep.equal({ a: 1 });
done();
});
});

it('throws on sample with response modify', function (done) {

var handler = function (request, reply) {

return reply({ a: 1, b: 2 });
};

var server = new Hapi.Server({ debug: false });
server.connection();
expect(function () {

server.route({
method: 'GET',
path: '/',
config: {
response: {
schema: Joi.object({
a: Joi.number()
}).options({ stripUnknown: true }),
modify: true,
sample: 90
}
},
handler: handler
});
}).to.throw(/modify conflict with forbidden peer sample/);
done();
});

it('validates response using custom validation function', function (done) {

var i = 0;
Expand Down

0 comments on commit f75a5fe

Please sign in to comment.