Yup schema won't add 'nullable' to optional fields.
See original GitHub issueWhen 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:
- Created a year ago
- Comments:9 (6 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@adamwdennis Thanks. I confirmed the problem.
https://codesandbox.io/s/yup-forked-z4fgyw?file=/src/index.js
I will fix this
Thanks for the above. Is
zod
an immediate, working alternative? I’m using thegraphql-codegen-typescript-validation-schema
with yup for Formik validation. I’ve found a plugin that can help me get azod
schema working with Formik, but when I use generate usingschema: zup
, I get errors within fields which are arrays of required types.