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

See original GitHub issue

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()
  })
}

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
Code-Hexcommented, May 9, 2022

@adamwdennis Thanks. I confirmed the problem.

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

I will fix this

1reaction
adamwdenniscommented, May 9, 2022

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

yup.number().nullable() does not allow null value #500 - GitHub
In my case, the value is an empty string, as a result of clearing an input field. I have the field defined as...
Read more >
Optional field validation in Yup schema - Stack Overflow
Its working fine, you have condition if the field is left empty then it will not validate, else it will validate because field...
Read more >
Validating Optional Objects with Yup - Object Partners
First, notRequired simply makes is so that undefined values do not fail validation. In the case of object types, Yup has a default...
Read more >
Yup | Best of JS
Yup is a schema builder for runtime value parsing and validation. ... Mark the schema as required, which will not allow undefined or...
Read more >
Using nullability in GraphQL
When you try to return a null value in a resolver for a non-null field, the null result bubbles up to the nearest...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found