diff --git a/types/hapi/index.d.ts b/types/hapi/index.d.ts index 40fae44148..8dca27d66e 100644 --- a/types/hapi/index.d.ts +++ b/types/hapi/index.d.ts @@ -35,8 +35,11 @@ import domain = require("domain"); import * as Boom from 'boom'; import { ValidationOptions as JoiValidationOptions, - Schema 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'; @@ -701,7 +704,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; } @@ -1350,7 +1353,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 @@ -1359,7 +1362,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; } @@ -1445,7 +1448,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..f160641016 --- /dev/null +++ b/types/hapi/test/route/validate.ts @@ -0,0 +1,20 @@ + +// 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(), + }, + payload: Joi.object().required(), + query: { + providerId: Joi.string(), + }, +}; + +const config: Hapi.RouteAdditionalConfigurationOptions = { + validate, +}; 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); }); diff --git a/types/hapi/tsconfig.json b/types/hapi/tsconfig.json index 5338f169d3..ef8e454667 100644 --- a/types/hapi/tsconfig.json +++ b/types/hapi/tsconfig.json @@ -48,6 +48,7 @@ "test/route/plugins.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",