Skip to content

Commit 620e443

Browse files
author
Eran Hammer
committed
Merge pull request hapijs#2439 from hapijs/responseSchemaContext
pass context to response schema validation
2 parents 4fc3fbe + 85c82cf commit 620e443

File tree

3 files changed

+56
-4
lines changed

3 files changed

+56
-4
lines changed

API.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2148,10 +2148,10 @@ following options:
21482148
expressed as one of:
21492149
- `true` - any payload allowed (no validation performed). This is the default.
21502150
- `false` - no payload allowed.
2151-
- a [Joi](http://github.com/hapijs/joi) validation object.
2151+
- a [Joi](http://github.com/hapijs/joi) validation object. This will receive the request's headers, params, query, payload, and auth credentials and isAuthenticated flags as context.
21522152
- a validation function using the signature `function(value, options, next)` where:
21532153
- `value` - the object containing the response object.
2154-
- `options` - the server validation options.
2154+
- `options` - the server validation options, merged with an object containing the request's headers, params, payload, and auth credentials object and isAuthenticated flag.
21552155
- `next(err)` - the callback function called when validation is completed.
21562156
- `status` - HTTP status-code-specific validation rules. The `status` key is set to an
21572157
object where each key is a 3 digit HTTP status code and the value has the same

lib/validation.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,12 +194,27 @@ exports.response = function (request, next) {
194194
return next(Boom.badImplementation(err.message));
195195
};
196196

197+
var localOptions = {
198+
context: {
199+
headers: request.headers,
200+
params: request.params,
201+
query: request.query,
202+
payload: request.payload,
203+
auth: {
204+
isAuthenticated: request.auth.isAuthenticated,
205+
credentials: request.auth.credentials
206+
}
207+
}
208+
};
209+
210+
Hoek.merge(localOptions, request.route.settings.response.options);
211+
197212
if (typeof schema !== 'function') {
198-
return Joi.validate(source, schema, request.route.settings.response.options, postValidate);
213+
return Joi.validate(source, schema, localOptions, postValidate);
199214
}
200215

201216
request._protect.run('validate:response', postValidate, function (exit) {
202217

203-
return schema(source, request.route.settings.response.options, exit);
218+
return schema(source, localOptions, exit);
204219
});
205220
};

test/validation.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,43 @@ describe('validation', function () {
753753
});
754754
});
755755

756+
it('validates response with context', function (done) {
757+
758+
var i = 0;
759+
var handler = function (request, reply) {
760+
761+
return reply({ some: 'thing', more: 'stuff' });
762+
};
763+
764+
var server = new Hapi.Server({ debug: false });
765+
server.connection();
766+
server.route({
767+
method: 'GET',
768+
path: '/',
769+
config: {
770+
response: {
771+
schema: Joi.object({
772+
some: Joi.string(),
773+
more: Joi.string()
774+
}).when('$query.user', { is: 'admin', otherwise: Joi.object({ more: Joi.forbidden() }) })
775+
}
776+
},
777+
handler: handler
778+
});
779+
780+
server.inject('/?user=admin', function (res) {
781+
782+
expect(res.statusCode).to.equal(200);
783+
expect(res.payload).to.equal('{"some":"thing","more":"stuff"}');
784+
785+
server.inject('/?user=test', function (res) {
786+
787+
expect(res.statusCode).to.equal(500);
788+
done();
789+
});
790+
});
791+
});
792+
756793
it('validates error response', function (done) {
757794

758795
var i = 0;

0 commit comments

Comments
 (0)