Skip to content

Yup schema won't add 'nullable' to optional fields. #64

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

Closed
adamwdennis opened this issue May 9, 2022 · 10 comments
Closed

Yup schema won't add 'nullable' to optional fields. #64

adamwdennis opened this issue May 9, 2022 · 10 comments
Labels
good first issue Good for newcomers pending Pending because of dependence on something

Comments

@adamwdennis
Copy link

adamwdennis commented May 9, 2022

When I run the generator, my Yup schema does not add .nullish() to any optional fields.

my-type.ts

@InputType
export class MyTypeInput {

  @Field(() => String)
  @IsString()
  @IsDefined()
  name!: string;

  @Field(() => ID, { nullable: true })
  @IsString()
  @IsOptional()
  imageId?: string;
}

schema.graphql

input MyTypeInput {
  name: String!
  imageId: ID
}

generated-operations.ts

export type MyTypeInput = {
  imageId?: InputMaybe<Scalars['ID']>;
  name: Scalars['String'];
}

Expected Result

generated-validation-schema.ts

export function MyTypeInputSchema(): yup.SchemaOf<MyTypeInput> {
  return yup.object({
    imageId: yup.string().nullable(),  // <----- this `nullable` is missing in the actual, below
    name: yup.string().defined()
  })
}

Actual Result

generated-validation-schema.ts

export function MyTypeInputSchema(): yup.SchemaOf<MyTypeInput> {
  return yup.object({
    imageId: yup.string(),
    name: yup.string().defined()
  })
}
@github-actions github-actions bot added the good first issue Good for newcomers label May 9, 2022
@adamwdennis adamwdennis changed the title Yup schema won't add 'nullish' to optional fields. Yup schema won't add 'nullable' to optional fields. May 9, 2022
@Code-Hex
Copy link
Owner

Code-Hex commented May 9, 2022

@adamwdennis Thanks to open issue!

Could you show me your GraphQL schema which is generated from your ts file? Thanks.

@adamwdennis
Copy link
Author

Hi @Code-Hex -- thanks for the quick response here... I've updated the description, per your request, with a schema.graphql section.

@Code-Hex
Copy link
Owner

Code-Hex commented May 9, 2022

@adamwdennis Thanks. I confirmed the problem.

https://codesandbox.io/s/yup-forked-z4fgyw?file=/src/index.js

I will fix this

@Code-Hex
Copy link
Owner

Code-Hex commented May 9, 2022

I remember why this is happening.
The type currently used (yup.SchamaOf) does not support nullable arrays.

jquense/yup#1440 (comment)

If this fix is made now, primitives will not have the problem, but arrays will not compile.
It is now implemented as a workaround.

What do you think, should we wait until the 1.0.0 release?

@Code-Hex
Copy link
Owner

Code-Hex commented May 9, 2022

Note (my question issue for next): jquense/yup#1658

@adamwdennis
Copy link
Author

Thanks for the above. Is zod an immediate, working alternative? I'm using the graphql-codegen-typescript-validation-schema with yup for Formik validation. I've found a plugin that can help me get a zod schema working with Formik, but when I use generate using schema: zup, I get errors within fields which are arrays of required types.

@Code-Hex
Copy link
Owner

@adamwdennis Yes, I think so. zod is good one.

Is zod an immediate, working alternative

I'm sorry this plugin does not support for zup.
see https://github.com/Code-Hex/graphql-codegen-typescript-validation-schema#config-api-reference

I use generate using schema: zup, I get errors within fields which are arrays of required types.

@Code-Hex Code-Hex added the pending Pending because of dependence on something label Jul 29, 2022
@qwexvf
Copy link

qwexvf commented Oct 6, 2022

@Code-Hex Hi. Thanks for the amazing library btw! Does this still a progressing issue?
I'm trying to achieve the result below ↓

I currently have a input like below. (forgive me for using a language-specific implementation input object)

  input_object :update_input do
    field(:id, non_null(:id))
    field(:email, :string)
    field(:name, :string)
  end

This is what codegen generated for me which is correct to my input defined on the server.

export type UpdateInput = {
    id: Scalars["ID"] # ID is required
    name?: InputMaybe<Scalars["String"]> # this is nullable
    email?: InputMaybe<Scalars["String"]> # this is nullable
}

This is what I got from your codegen.
field email and name are required whereas the generated types have optional and nullable options.

export function UpdateInputSchema(): yup.SchemaOf<UpdateInput> {
    return yup.object({
        id: yup.string().defined(),
        email: yup.string(),
        name: yup.string(),
    })
}

These are the results I expected. I might not even need the optional field but can I somehow get the nullable() ?

return yup.object({
  id: yup.string().defined(),
  email: yup.string().nullable(),
   name: yup.string().nullable(),
})

could you help me achieve this?
Thanks!

@Code-Hex
Copy link
Owner

@qwexvf Hi 👋

I think the code that is you want will be occurred compilation error by yup.SchemaOf<UpdateInput>

return yup.object({
  id: yup.string().defined(),
  email: yup.string().nullable(),
   name: yup.string().nullable(),
})

By the way, Could you share me your GraphQL schema (only UpdateInput)?

@Code-Hex
Copy link
Owner

I think this problem has been resolved at #351

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers pending Pending because of dependence on something
Projects
None yet
Development

No branches or pull requests

3 participants