From 678cc80ff5bcf9f660d4784e810134c60a0d7b03 Mon Sep 17 00:00:00 2001 From: Alexander James Phillips Date: Wed, 17 May 2017 23:50:18 +0100 Subject: [PATCH 1/3] Attempt to fix correct JoiValidationObject for hapi route validate option --- types/hapi/index.d.ts | 9 +++++---- types/hapi/test/route/validate.ts | 19 +++++++++++++++++++ types/hapi/tsconfig.json | 1 + 3 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 types/hapi/test/route/validate.ts diff --git a/types/hapi/index.d.ts b/types/hapi/index.d.ts index 3567e50c34..644237f817 100644 --- a/types/hapi/index.d.ts +++ b/types/hapi/index.d.ts @@ -35,7 +35,8 @@ import domain = require("domain"); import * as Boom from 'boom'; import { ValidationOptions as JoiValidationOptions, - Schema as JoiValidationObject, + // TODO check JoiValidationObject is correct for "a Joi validation object" + SchemaMap as JoiValidationObject, } from 'joi'; import * as Catbox from 'catbox'; @@ -1304,7 +1305,7 @@ export interface RouteResponseConfigurationObject { * and * For context see RouteAdditionalConfigurationOptions > response > status */ -export type RouteResponseConfigurationScheme = boolean | JoiValidationObject | ValidationFunctionForRouteReponse; +export type RouteResponseConfigurationScheme = boolean | JoiValidationObject | ValidationFunctionForRouteResponse; /** * see RouteResponseConfigurationScheme @@ -1313,7 +1314,7 @@ export type RouteResponseConfigurationScheme = boolean | JoiValidationObject | V * TODO check `options: JoiValidationOptions` is correct * Also see ValidationFunctionForRouteValidate */ -export interface ValidationFunctionForRouteReponse { +export interface ValidationFunctionForRouteResponse { (value: Response, options: JoiValidationOptions, next: ContinuationFunction): void; } @@ -1399,7 +1400,7 @@ export interface RouteValidationConfigurationObject { * TODO check `value: Response` is correct as it says "**the object containing** the response object." not just "the response object". * TODO check `options: JoiValidationOptions` is correct * TODO type of the returned value? - * Also see ValidationFunctionForRouteReponse + * Also see ValidationFunctionForRouteResponse * @param value - the object containing the request headers. * @param options - the server validation options. * @param next(err, value) - the callback function called when validation is completed. diff --git a/types/hapi/test/route/validate.ts b/types/hapi/test/route/validate.ts new file mode 100644 index 0000000000..47abc77a96 --- /dev/null +++ b/types/hapi/test/route/validate.ts @@ -0,0 +1,19 @@ + +// Added from: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/16065#issuecomment-302216131 + +import * as Hapi from 'hapi'; +import * as Joi from 'joi'; + +const validate: Hapi.RouteValidationConfigurationObject = { + headers: true, + params: { + id: Joi.string(), + }, + query: { + providerId: Joi.string(), + }, +} + +const config: Hapi.RouteAdditionalConfigurationOptions = { + validate, +}; diff --git a/types/hapi/tsconfig.json b/types/hapi/tsconfig.json index f8d5b055e2..f811a2f8fb 100644 --- a/types/hapi/tsconfig.json +++ b/types/hapi/tsconfig.json @@ -47,6 +47,7 @@ "test/route/handler.ts", "test/route/prerequisites.ts", "test/route/public-interface.ts", + "test/route/validate.ts", "test/server/app.ts", "test/server/auth.ts", "test/server/bind.ts", From bbcb50a4e160d9f72ce8aa2d0cf21b40062a78ea Mon Sep 17 00:00:00 2001 From: Alexander James Phillips Date: Thu, 18 May 2017 10:49:57 +0100 Subject: [PATCH 2/3] Update hapi route validate to allow joi Schema SchemaMap or array of those --- types/hapi/index.d.ts | 6 ++++-- types/hapi/test/route/validate.ts | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/types/hapi/index.d.ts b/types/hapi/index.d.ts index 644237f817..26b54b0ace 100644 --- a/types/hapi/index.d.ts +++ b/types/hapi/index.d.ts @@ -35,9 +35,11 @@ import domain = require("domain"); import * as Boom from 'boom'; import { ValidationOptions as JoiValidationOptions, - // TODO check JoiValidationObject is correct for "a Joi validation object" - SchemaMap as JoiValidationObject, + SchemaMap as JoiSchemaMap, + Schema as JoiSchema, } from 'joi'; +// TODO check JoiValidationObject is correct for "a Joi validation object" +type JoiValidationObject = JoiSchema | JoiSchemaMap | (JoiSchema | JoiSchemaMap)[]; import * as Catbox from 'catbox'; import {MimosOptions} from 'mimos'; diff --git a/types/hapi/test/route/validate.ts b/types/hapi/test/route/validate.ts index 47abc77a96..f160641016 100644 --- a/types/hapi/test/route/validate.ts +++ b/types/hapi/test/route/validate.ts @@ -9,10 +9,11 @@ const validate: Hapi.RouteValidationConfigurationObject = { params: { id: Joi.string(), }, + payload: Joi.object().required(), query: { providerId: Joi.string(), }, -} +}; const config: Hapi.RouteAdditionalConfigurationOptions = { validate, From cec538087c579d5281c233c03613e9e93f200512 Mon Sep 17 00:00:00 2001 From: Alexander James Phillips Date: Thu, 18 May 2017 23:26:54 +0100 Subject: [PATCH 3/3] Correct inject result type --- types/hapi/index.d.ts | 2 +- types/hapi/test/server/inject.ts | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/types/hapi/index.d.ts b/types/hapi/index.d.ts index 26b54b0ace..0249d343cb 100644 --- a/types/hapi/index.d.ts +++ b/types/hapi/index.d.ts @@ -698,7 +698,7 @@ export interface InjectedRequestOptions extends Shot.RequestOptions { */ export interface InjectedResponseObject extends Shot.ResponseObject { /** the raw handler response (e.g. when not a stream or a view) before it is serialized for transmission. If not available, the value is set to payload. Useful for inspection and reuse of the internal objects returned (instead of parsing the response string). */ - result: Shot.ResponseObject | string; + result: Object | string; /** the request object. */ request: InjectedRequestOptions; } diff --git a/types/hapi/test/server/inject.ts b/types/hapi/test/server/inject.ts index 130af7489b..7010faa3dc 100644 --- a/types/hapi/test/server/inject.ts +++ b/types/hapi/test/server/inject.ts @@ -13,6 +13,7 @@ const handler: Hapi.RouteHandler = function (request, reply) { server.route({ method: 'GET', path: '/', handler: handler }); server.inject('/', (res) => { - + const num: number = res.statusCode; + const result = res.result as {aField: string}; console.log(res.result); });