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

Bugs with yup v1 and arrays of non nullable #359

Open
erwan-joly opened this issue Apr 26, 2023 · 7 comments
Open

Bugs with yup v1 and arrays of non nullable #359

erwan-joly opened this issue Apr 26, 2023 · 7 comments
Labels
pending Pending because of dependence on something

Comments

@erwan-joly
Copy link
Contributor

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.

@erwan-joly
Copy link
Contributor Author

may be related to yup jquense/yup#1890 ?

@Code-Hex
Copy link
Owner

Code-Hex commented May 5, 2023

@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.

@Code-Hex Code-Hex added the pending Pending because of dependence on something label May 5, 2023
@erwan-joly
Copy link
Contributor Author

erwan-joly commented May 5, 2023

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!
}

@Code-Hex
Copy link
Owner

Code-Hex commented May 13, 2023

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

@erwan-joly
Copy link
Contributor Author

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

will test that asap

@erwan-joly
Copy link
Contributor Author

erwan-joly commented May 23, 2023

@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

@Code-Hex
Copy link
Owner

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

erwan-joly added a commit to erwan-joly/graphql-codegen-typescript-validation-schema that referenced this issue Jun 8, 2023
erwan-joly added a commit to erwan-joly/graphql-codegen-typescript-validation-schema that referenced this issue Jun 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pending Pending because of dependence on something
Projects
None yet
2 participants