Skip to content

Commit

Permalink
fix: address generics type change and joi-extract-type issues (#26)
Browse files Browse the repository at this point in the history
* fix: address changes in joi v16 types

* chore: bump verison to 4.0.2

* docs: changelog for 4.0.2

* fix: should still extend

* fix: remove joi-extract-type temporarily
  • Loading branch information
evanshortiss authored Nov 11, 2019
1 parent 2b9d31e commit 52def36
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
40 changes: 38 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<HelloRequestSchema>, 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'
Expand Down
9 changes: 6 additions & 3 deletions example/typescript/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
ContainerTypes
} from '../../express-joi-validation'
import { Router } from 'express'
import 'joi-extract-type'

const route = Router()
const validator = createValidator()
Expand All @@ -17,11 +16,15 @@ const schema = Joi.object({
})

interface HelloGetRequestSchema extends ValidatedRequestSchema {
[ContainerTypes.Query]: Joi.extractType<typeof schema>
[ContainerTypes.Query]: {
name: string
}
}

interface HelloPostRequestSchema extends ValidatedRequestSchema {
[ContainerTypes.Fields]: Joi.extractType<typeof schema>
[ContainerTypes.Fields]: {
name: string
}
}

// curl http://localhost:3030/hello/?name=express
Expand Down
2 changes: 1 addition & 1 deletion express-joi-validation.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any> {
export interface ExpressJoiError extends Joi.ValidationResult {
type: ContainerTypes
}

Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 52def36

Please sign in to comment.