Skip to content

Can I use this with number()?Β #30

@danielbrauer

Description

@danielbrauer

This validator looks like what I need, but as soon as I try to put a number in the schema I get horrible errors. Here is reproduction code, just slightly modified version of your type extraction example:

import * as Joi from '@hapi/joi'
import * as express from 'express'
import {
  ContainerTypes,
  ValidatedRequest,
  ValidatedRequestSchema,
  createValidator
} from 'express-joi-validation'

const app = express()
const validator = createValidator()

const querySchema = Joi.object({
  name: Joi.number().required()
})

interface HelloRequestSchema extends ValidatedRequestSchema {
  [ContainerTypes.Query]: {
    name: number
  }
}

app.get(
  '/hello',
  validator.query(querySchema),
  (req: ValidatedRequest<HelloRequestSchema>, res) => { // No overload matches this call
    res.end(`Hello ${req.query.name}!`)
  }
)

This produces the error:

No overload matches this call.
  Overload 1 of 4, '(path: PathParams, ...handlers: RequestHandler<any, any, any, ParsedQs>[]): Express', gave the following error.
    Argument of type '(req: ValidatedRequest<HelloRequestSchema>, res: Response<any>) => void' is not assignable to parameter of type 'RequestHandler<any, any, any, ParsedQs>'.
      Types of parameters 'req' and 'req' are incompatible.
        Type 'Request<any, any, any, ParsedQs>' is not assignable to type 'ValidatedRequest<HelloRequestSchema>'.
          Types of property 'query' are incompatible.
            Property 'name' is missing in type 'ParsedQs' but required in type '{ name: number; }'.
  Overload 2 of 4, '(path: PathParams, ...handlers: RequestHandlerParams<any, any, any, ParsedQs>[]): Express', gave the following error.
    Argument of type '(req: ValidatedRequest<HelloRequestSchema>, res: Response<any>) => void' is not assignable to parameter of type 'RequestHandlerParams<any, any, any, ParsedQs>'.
      Type '(req: ValidatedRequest<HelloRequestSchema>, res: Response<any>) => void' is not assignable to type 'RequestHandler<any, any, any, ParsedQs>'.
        Types of parameters 'req' and 'req' are incompatible.
          Type 'Request<any, any, any, ParsedQs>' is not assignable to type 'ValidatedRequest<HelloRequestSchema>'.ts(2769)

I am using:

"@hapi/joi": "15",
"@types/hapi__joi": "15",
"express-joi-validation": "^4.0.3",

Updated to remove type extraction: the issue happens even without it

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions