diff --git a/CHANGELOG.md b/CHANGELOG.md index 63aa529..d97e3fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ ## CHANGELOG Date format is DD/MM/YYYY +## 4.0.2 (12/11/2019) +* Apply a fix for compatibility with Joi v16 typings. + ## 4.0.1 (24/09/2019) * Remove outdated "joi" option in README diff --git a/README.md b/README.md index 1af3d0c..0d2edfc 100644 --- a/README.md +++ b/README.md @@ -76,8 +76,44 @@ For TypeScript a helper `ValidatedRequest` and `express.Request` type and allows you to pass a schema using generics to ensure type safety in your handler function. -One downside to this is that there's some duplication. You can minimise this -duplication by using [joi-extract-type](https://github.com/TCMiranda/joi-extract-type/). +```ts +import * as Joi from '@hapi/joi' +import * as express from 'express' +import { + // Use this as a replacement for express.Request + ValidatedRequest, + // Extend from this to define a valid schema type/interface + ValidatedRequestSchema, + // Creates a validator that generates middlewares + createValidator +} from 'express-joi-validation' + +const app = express() +const validator = createValidator() + +const querySchema = Joi.object({ + name: Joi.string().required() +}) + +interface HelloRequestSchema extends ValidatedRequestSchema { + [ContainerTypes.Query]: { + name: string + } +} + +app.get( + '/hello', + validator.query(querySchema), + (req: ValidatedRequest, res) => { + // Woohoo, type safety and intellisense for req.query! + res.end(`Hello ${req.query.name}!`) + } +) +``` + +You can minimise some duplication by using [joi-extract-type](https://github.com/TCMiranda/joi-extract-type/). + +_NOTE: this does not work with Joi v16+ at the moment. See [this issue](https://github.com/TCMiranda/joi-extract-type/issues/23)._ ```ts import * as Joi from '@hapi/joi' diff --git a/example/typescript/route.ts b/example/typescript/route.ts index d71bc33..c4d078c 100644 --- a/example/typescript/route.ts +++ b/example/typescript/route.ts @@ -8,7 +8,6 @@ import { ContainerTypes } from '../../express-joi-validation' import { Router } from 'express' -import 'joi-extract-type' const route = Router() const validator = createValidator() @@ -17,11 +16,15 @@ const schema = Joi.object({ }) interface HelloGetRequestSchema extends ValidatedRequestSchema { - [ContainerTypes.Query]: Joi.extractType + [ContainerTypes.Query]: { + name: string + } } interface HelloPostRequestSchema extends ValidatedRequestSchema { - [ContainerTypes.Fields]: Joi.extractType + [ContainerTypes.Fields]: { + name: string + } } // curl http://localhost:3030/hello/?name=express diff --git a/express-joi-validation.d.ts b/express-joi-validation.d.ts index 350ae44..33428ce 100644 --- a/express-joi-validation.d.ts +++ b/express-joi-validation.d.ts @@ -24,7 +24,7 @@ export enum ContainerTypes { * Use this in you express error handler if you've set *passError* to true * when calling *createValidator* */ -export interface ExpressJoiError extends Joi.ValidationResult { +export interface ExpressJoiError extends Joi.ValidationResult { type: ContainerTypes } diff --git a/package.json b/package.json index 7db69fc..bfdc97f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "express-joi-validation", - "version": "4.0.1", + "version": "4.0.2", "description": "validate express application inputs and parameters using joi", "main": "express-joi-validation.js", "scripts": { @@ -29,10 +29,10 @@ "author": "Evan Shortiss", "license": "MIT", "devDependencies": { - "@hapi/joi": "~16.1.2", + "@hapi/joi": "~16.1.7", "@types/express": "~4.0.39", "@types/express-formidable": "~1.0.4", - "@types/hapi__joi": "~15.0.2", + "@types/hapi__joi": "~16.0.3", "@types/node": "~10.14.18", "body-parser": "~1.18.3", "chai": "~3.5.0",