diff --git a/lib/route.js b/lib/route.js index 642c4d866..ab18b93b3 100755 --- a/lib/route.js +++ b/lib/route.js @@ -37,10 +37,10 @@ exports = module.exports = internals.Route = class { const method = route.method.toLowerCase(); Hoek.assert(method !== 'head', 'Cannot set HEAD route:', route.path); - const path = (realm.modifiers.route.prefix ? realm.modifiers.route.prefix + (route.path !== '/' ? route.path : '') : route.path); + const path = realm.modifiers.route.prefix ? realm.modifiers.route.prefix + (route.path !== '/' ? route.path : '') : route.path; Hoek.assert(path === '/' || path[path.length - 1] !== '/' || !core.settings.router.stripTrailingSlash, 'Path cannot end with a trailing slash when configured to strip:', route.method, route.path); - const vhost = (realm.modifiers.route.vhost || route.vhost); + const vhost = realm.modifiers.route.vhost || route.vhost; // Set identifying members (assert) @@ -64,7 +64,7 @@ exports = module.exports = internals.Route = class { // Rules this._assert(!route.rules || !config.rules, 'Route rules can only appear once'); // XOR - const rules = (route.rules || config.rules); + const rules = route.rules || config.rules; const rulesConfig = internals.rules(rules, { method, path, vhost }, server); delete config.rules; @@ -85,7 +85,7 @@ exports = module.exports = internals.Route = class { // Validate timeouts - const socketTimeout = (this.settings.timeout.socket === undefined ? 2 * 60 * 1000 : this.settings.timeout.socket); + const socketTimeout = this.settings.timeout.socket === undefined ? 2 * 60 * 1000 : this.settings.timeout.socket; this._assert(!this.settings.timeout.server || !socketTimeout || this.settings.timeout.server < socketTimeout, 'Server timeout must be shorter than socket timeout'); this._assert(!this.settings.payload.timeout || !socketTimeout || this.settings.payload.timeout < socketTimeout, 'Payload timeout must be shorter than socket timeout'); @@ -136,7 +136,7 @@ exports = module.exports = internals.Route = class { // Authentication configuration - this.settings.auth = (this._special ? false : this._core.auth._setupRoute(this.settings.auth, path)); + this.settings.auth = this._special ? false : this._core.auth._setupRoute(this.settings.auth, path); // Cache @@ -227,7 +227,7 @@ exports = module.exports = internals.Route = class { } if (this._special) { - this._postCycle = (this._extensions.onPreResponse.nodes ? [this._extensions.onPreResponse] : []); + this._postCycle = this._extensions.onPreResponse.nodes ? [this._extensions.onPreResponse] : []; this._buildMarshalCycle(); return; } @@ -525,10 +525,7 @@ internals.rules = function (rules, info, server) { let realm = server.realm; while (realm) { if (realm._rules) { - const source = !realm._rules.settings.validate - ? rules - : Joi.attempt(rules, realm._rules.settings.validate.schema, realm._rules.settings.validate.options); - + const source = !realm._rules.settings.validate ? rules : Joi.attempt(rules, realm._rules.settings.validate.schema, realm._rules.settings.validate.options); const config = realm._rules.processor(source, info); if (config) { configs.unshift(config); diff --git a/test/validation.js b/test/validation.js index 2d32ef70a..97ff28bf1 100755 --- a/test/validation.js +++ b/test/validation.js @@ -1049,12 +1049,6 @@ describe('validation', () => { it('validates response with context', async () => { - const schema = Joi.object({ - some: Joi.string(), - more: Joi.string() - }) - .when('$query.user', { not: 'admin', then: Joi.object({ more: Joi.forbidden() }) }); - const server = Hapi.server({ debug: false }); server.validator(Joi); server.route({ @@ -1062,17 +1056,19 @@ describe('validation', () => { path: '/', options: { response: { - schema + schema: Joi.object({ + some: Joi.string(), + more: Joi.string() + }) + .when('$query.user', { not: 'admin', then: Joi.object({ more: Joi.forbidden() }) }) } }, handler: () => ({ some: 'thing', more: 'stuff' }) }); - schema.validate({ some: 'thing', more: 'stuff' }, { context: { query: { user: 'test' } } }); - - //const res1 = await server.inject('/?user=admin'); - //expect(res1.statusCode).to.equal(200); - //expect(res1.payload).to.equal('{"some":"thing","more":"stuff"}'); + const res1 = await server.inject('/?user=admin'); + expect(res1.statusCode).to.equal(200); + expect(res1.payload).to.equal('{"some":"thing","more":"stuff"}'); const res2 = await server.inject('/?user=test'); expect(res2.statusCode).to.equal(500);