Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incompatible type expected for object schemas nested in strict object schemas #2764

Closed
bryandrenner opened this issue Apr 12, 2022 · 4 comments · Fixed by #2836
Closed

Incompatible type expected for object schemas nested in strict object schemas #2764

bryandrenner opened this issue Apr 12, 2022 · 4 comments · Fixed by #2836
Assignees
Labels
bug Bug or defect types TypeScript type definitions
Milestone

Comments

@bryandrenner
Copy link

Context

  • node version: 14.19.1
  • module version with issue: 17.5.0, 17.6.0
  • last module version without issue: 17.4.3
  • environment (e.g. node, browser, native): Node
  • used with (e.g. hapi application, another framework, standalone, ...): standalone

What are you trying to achieve or the steps to reproduce?

When I nest an object schema inside of another, strict object schema, I expect no type issues. For example, the following code compiles in v17.4.3:

import Joi from 'joi'

type Name = {
    first: string
    last: string
}

const nameSchema = Joi.object<Name>({
    first: Joi.string().required(),
    last: Joi.string().required()
})

type Customer = {
    name: Name
}

const customerSchema = Joi.object<Customer, true>({
    name: nameSchema
})

What was the result you got?

As of v17.5.0, the above code does not compile. The compiler reports the following:

demo.ts:18:5 - error TS2322: Type 'ObjectSchema<Name>' is not assignable to type 'ObjectSchema<StrictSchemaMap<Name>>'.
  Types of property 'concat' are incompatible.
    Type '(schema: ObjectSchema<Name>) => ObjectSchema<Name>' is not assignable to type '(schema: ObjectSchema<StrictSchemaMap<Name>>) => ObjectSchema<StrictSchemaMap<Name>>'.
      Types of parameters 'schema' and 'schema' are incompatible.
        Type 'ObjectSchema<StrictSchemaMap<Name>>' is not assignable to type 'ObjectSchema<Name>'.
          The types returned by 'validate(...)' are incompatible between these types.
            Type 'ValidationResult<StrictSchemaMap<Name>>' is not assignable to type 'ValidationResult<Name>'.
              Type '{ error: undefined; warning?: ValidationError | undefined; value: StrictSchemaMap<Name>; }' is not assignable to type 'ValidationResult<Name>'.
                Type '{ error: undefined; warning?: ValidationError | undefined; value: StrictSchemaMap<Name>; }' is not assignable to type '{ error: undefined; warning?: ValidationError | undefined; value: Name; }'.
                  Types of property 'value' are incompatible.
                    Type 'StrictSchemaMap<Name>' is not assignable to type 'Name'.

18     name: nameSchema

What result did you expect?

I would expect the code to compile without issues.

If I change this line:

const customerSchema = Joi.object<Customer, true>({

...to this:

const customerSchema = Joi.object<Customer>({

...then the code compiles, but I lose the safety of strict object schemas.

@dunyakirkali
Copy link

dunyakirkali commented Apr 25, 2022

I have the same issue

It used to work in 17.4.2 but stoped working in 17.6.0

Just by looking at the changelog I don't see what might have caused the issue

@vdvleon
Copy link

vdvleon commented May 9, 2022

I am experiencing the same problem. It seems the return type of joi.object<Type, true>() returns an ObjectSchema without the strict typing applied on its generic type.

@LeoAso
Copy link

LeoAso commented May 21, 2022

The issue comes from #2627.

That PR added isStrict to Joi.object, but then requires that any schema nested inside a strict schema must have the type ObjectSchema<StrictSchemaMap<T>>, which isn't possible because Joi.object returns ObjectSchema<T> whether it is strict or not. Even the example given in the PR doesn't work anymore with those changes.

I think I'll open a new PR to fix the issue.

@matgott
Copy link

matgott commented Sep 15, 2022

Hi folks! I see two PRs open to fix this issue.

Is there any ETA for this? Thank you

@Marsup Marsup added this to the 17.6.1 milestone Sep 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Bug or defect types TypeScript type definitions
Projects
None yet
6 participants