Skip to content

Bugs with yup v1 and arrays of non nullable #359

Closed

Description

@erwan-joly

It seems like their is some issues with array and nullable types

export function UpdateAssessmentInputSchema(): yup.ObjectSchema<UpdateAssessmentInput> {
    return yup.object({
        answers: yup.array(yup.lazy(() => QuestionAnswerInputSchema().nonNullable())).defined(),
        domainIndex: yup.number().defined().nonNullable(),
        encodedId: yup.string().defined().nonNullable(),
        groupIndex: yup.number().defined().nonNullable(),
        previous: yup.boolean().defined().nonNullable(),
        save: yup.boolean().defined().nonNullable()
    })
}

export type UpdateAssessmentInput = {
  answers: Array<QuestionAnswerInput>;
  domainIndex: Scalars['Int'];
  encodedId: Scalars['String'];
  groupIndex: Scalars['Int'];
  previous: Scalars['Boolean'];
  save: Scalars['Boolean'];
};

export function QuestionAnswerInputSchema(): yup.ObjectSchema<QuestionAnswerInput> {
    return yup.object({
        answer: yup.string().defined().nullable().optional(),
        index: yup.number().defined().nonNullable(),
        multichoiceAnswers: yup.array(yup.string().defined().nonNullable()).defined()
    })
}

Results in this error:

Error	TS2322	(TS) Type 'ObjectSchema<{ answers: { answer?: string; index?: number; multichoiceAnswers?: string[]; }[]; domainIndex: number; encodedId: string; groupIndex: number; previous: boolean; save: boolean; }, AnyObject, { ...; }, "">' is not assignable to type 'ObjectSchema<UpdateAssessmentInput, AnyObject, any, "">'.
  The types of 'default(...).fields.answers' are incompatible between these types.
    Type 'Reference<unknown> | ISchema<{ answer?: string; index?: number; multichoiceAnswers?: string[]; }[], AnyObject, any, any>' is not assignable to type 'Reference<unknown> | ISchema<QuestionAnswerInput[], AnyObject, any, any>'.
      Type 'ISchema<{ answer?: string; index?: number; multichoiceAnswers?: string[]; }[], AnyObject, any, any>' is not assignable to type 'Reference<unknown> | ISchema<QuestionAnswerInput[], AnyObject, any, any>'.
        Type 'ISchema<{ answer?: string; index?: number; multichoiceAnswers?: string[]; }[], AnyObject, any, any>' is not assignable to type 'ISchema<QuestionAnswerInput[], AnyObject, any, any>'.
          Type '{ answer?: string; index?: number; multichoiceAnswers?: string[]; }[]' is not assignable to type 'QuestionAnswerInput[]'.
            Type '{ answer?: string; index?: number; multichoiceAnswers?: string[]; }' is not assignable to type 'QuestionAnswerInput'.
              Property 'index' is optional in type '{ answer?: string; index?: number; multichoiceAnswers?: string[]; }' but required in type 'QuestionAnswerInput'.	

For some reasons it seems like the system believe all properties of QuestionAnswerInputSchema should be nullable but in the end gets some required property and fail.

Activity

  1. erwan-joly commented on Apr 27, 2023

    @erwan-joly
    Author

    may be related to yup jquense/yup#1890 ?

  2. Code-Hex commented on May 5, 2023

    @Code-Hex
    Owner

    @erwan-joly mmm, The link mentioned to Yup v0.32 not v1.

    Could you give me the QuestionAnswerInputSchema as GraphQL Schema?
    I want to check it in my local too.

  3. added
    pendingPending because of dependence on something
    on May 5, 2023
  4. erwan-joly commented on May 5, 2023

    @erwan-joly
    Author

    yep it's happening for me in v1 and was fine before the upgrade but thats the only thing close to my issue i could find.
    here the graphql part

    input QuestionAnswerInput {
      answer: String
      index: Int!
      multichoiceAnswers: [String!]!
    }
    
    input UpdateAssessmentInput {
      answers: [QuestionAnswerInput!]!
      domainIndex: Int!
      encodedId: String!
      groupIndex: Int!
      previous: Boolean!
      save: Boolean!
    }
    
    
  5. Code-Hex commented on May 13, 2023

    @Code-Hex
    Owner

    I tried this schema and type-checked with tsc --strict --noEmit example/yup/schemas.ts but I couldn't get build error.

    Do you use "strict": true, option in tsconfig.json?

    You can try it!

    1. Clone this repository on your local (main branch).
    2. Rewrite example/test.graphql with your provided schema.
    3. Run yarn generate to generate validation schema for example code.
    4. Run type check with yarn type-check:yup
  6. erwan-joly commented on May 13, 2023

    @erwan-joly
    Author

    I only use
    "noImplicitAny": true,
    "module": "ES2022",
    "target": "ES2022",
    "allowSyntheticDefaultImports": true,
    "jsx": "react",
    "allowJs": false,
    "moduleResolution": "node",
    "skipLibCheck": true

    will test that asap

  7. erwan-joly commented on May 23, 2023

    @erwan-joly
    Author

    @Code-Hex I've been able to reproduce the error
    this as test.graphql will fail yarn type-check:yup

    type InnerType {
      testScalar: MyScalar!
    }
    
    type OuterType {
      inner: InnerType
    }
    
    scalar MyScalar
    

    Seems to be due to nested type with custom scalar field. as This schema would not fire an error

    type OuterType {
      testScalar: MyScalar!
    }
    
    scalar MyScalar
    
  8. Code-Hex commented on May 27, 2023

    @Code-Hex
    Owner

    @erwan-joly Thanks! I submitted issue in yup
    jquense/yup#2013

  9. added 2 commits that reference this issue on Jun 8, 2023
    55ae41a
    6d29391
  10. Code-Hex commented on Apr 25, 2026

    @Code-Hex
    Owner

    Covered in #1423. The repo is now on Yup 1.7.1, and I added a strict TypeScript compile regression for arrays of non-null input objects so this case stays checked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    pendingPending because of dependence on something

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions