hapijs / joi

The most powerful data validation library for JS
Other
20.88k stars 1.51k forks source link

Got type error after upgrade from @hapi/joi 17.1 to joi 17.2 #2445

Closed wy193777 closed 3 years ago

wy193777 commented 4 years ago

Support plan

Context

How can we help?

Got type error on route.options.validate.query when using Joi.object()

const getChipsRoute: Hapi.ServerRoute = {
  method: "GET",
  path: "/chips",
  options: {
    handler: ChipInfoController.getChips,
    tags: ["api", "chip"],
    auth: AuthStrategies.ReadStrategies,
    description: "Returns the Chip Information by registerfield id.",
    validate: {
      headers: AuthHeaderValidator,
      query: Joi.object({  // got type error here
        page_number: Joi.number().default(1),
        size: Joi.number().default(100),
        sort_by: Joi.string().default("chip_name desc"),
      }),
      failAction: FailAction,
    },
    plugins: {
      "hapi-swagger": {
        responses: {
          200: {
            description: "Record Found!!",
          },
          404: {
            description: "No Record Found!!",
          },
        },
      },
    },
  },
};

Error

Type 'ObjectSchema<{ [x: string]: ...; }>' is not assignable to type 'RouteOptionsResponseSchema'.
  Type 'ObjectSchema<{ [x: string]: ...; }>' is not assignable to type 'SchemaMap<any>'.
    Index signature is missing in type 'ObjectSchema<{ [x: string]: ...; }>'.ts(2322)
index.d.ts(1709, 5): The expected type comes from property 'query' which is declared here on type 'RouteOptionsValidate'
hueniverse commented 4 years ago

Is it possible the hapi types you are using are referencing another joi types file and the two are in conflict?

wy193777 commented 4 years ago

I already uninstalled @type/hapijoi. But seems query -> RouteOptionsResponseSchema -> Schema still comes from @type/hapijoi.

hueniverse commented 4 years ago

Yeah, someone will have to fix it over there...

wy193777 commented 4 years ago

Other @hapi/xxx projects also use @hapi/hapi__joi as a dependency, I guess everyone of them need to be fixed?

hueniverse commented 4 years ago

No because those are internal usages.

wy193777 commented 4 years ago

After change @type/hapi__hapi's type file, I got the following error, do you have any idea on how to debug it? I don't know if this a problem on my code or on hapi itself.

.\Backend\node_modules\@hapi\hoek\lib\error.js:23
            Error.captureStackTrace(this, exports.assert);
                  ^
Error: Schema can only contain plain objects
    at new module.exports (.\Backend\node_modules\@hapi\hoek\lib\error.js:23:19)
    at module.exports (.\Backend\node_modules\@hapi\hoek\lib\assert.js:20:11)
    at Object.internals.schema (.\Backend\node_modules\joi\lib\compile.js:87:5)
    at Object.exports.schema (.\Backend\node_modules\joi\lib\compile.js:16:26)
    at internals.Base.$_compile (.\Backend\node_modules\joi\lib\base.js:645:24)
    at .\Backend\node_modules\joi\lib\types\keys.js:256:92
    at Object.exports.tryWithPath (.\Backend\node_modules\joi\lib\common.js:176:16)
    at internals.Base.method [as keys] (.\Backend\node_modules\joi\lib\types\keys.js:256:32)
    at Object.internals.schema (.\Backend\node_modules\joi\lib\compile.js:89:25)
    at Object.exports.schema (.\Backend\node_modules\joi\lib\compile.js:16:26)
wy193777 commented 4 years ago

The problem is on my code. But it's a little bit hard with this kind of error stack trace.

MiguelGonzales-WIV commented 2 years ago

Ever got it fixed?

wy193777 commented 2 years ago

After update dependencies the problem disappeared.

danzf commented 10 months ago

updating which dependency? @wy193777