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

pass context to response schema validation #2439

Merged
merged 2 commits into from
Mar 6, 2015
Merged
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
4 changes: 2 additions & 2 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -2148,10 +2148,10 @@ following options:
expressed as one of:
- `true` - any payload allowed (no validation performed). This is the default.
- `false` - no payload allowed.
- a [Joi](http://github.com/hapijs/joi) validation object.
- 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.
- a validation function using the signature `function(value, options, next)` where:
- `value` - the object containing the response object.
- `options` - the server validation options.
- `options` - the server validation options, merged with an object containing the request's headers, params, payload, and auth credentials object and isAuthenticated flag.
- `next(err)` - the callback function called when validation is completed.
- `status` - HTTP status-code-specific validation rules. The `status` key is set to an
object where each key is a 3 digit HTTP status code and the value has the same
Expand Down
19 changes: 17 additions & 2 deletions lib/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,27 @@ exports.response = function (request, next) {
return next(Boom.badImplementation(err.message));
};

var localOptions = {
context: {
headers: request.headers,
params: request.params,
query: request.query,
payload: request.payload,
auth: {
isAuthenticated: request.auth.isAuthenticated,
credentials: request.auth.credentials
}
}
};

Hoek.merge(localOptions, request.route.settings.response.options);

if (typeof schema !== 'function') {
return Joi.validate(source, schema, request.route.settings.response.options, postValidate);
return Joi.validate(source, schema, localOptions, postValidate);
}

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

return schema(source, request.route.settings.response.options, exit);
return schema(source, localOptions, exit);
});
};
37 changes: 37 additions & 0 deletions test/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,43 @@ describe('validation', function () {
});
});

it('validates response with context', function (done) {

var i = 0;
var handler = function (request, reply) {

return reply({ some: 'thing', more: 'stuff' });
};

var server = new Hapi.Server({ debug: false });
server.connection();
server.route({
method: 'GET',
path: '/',
config: {
response: {
schema: Joi.object({
some: Joi.string(),
more: Joi.string()
}).when('$query.user', { is: 'admin', otherwise: Joi.object({ more: Joi.forbidden() }) })
}
},
handler: handler
});

server.inject('/?user=admin', function (res) {

expect(res.statusCode).to.equal(200);
expect(res.payload).to.equal('{"some":"thing","more":"stuff"}');

server.inject('/?user=test', function (res) {

expect(res.statusCode).to.equal(500);
done();
});
});
});

it('validates error response', function (done) {

var i = 0;
Expand Down