Skip to content

Allow configuration of zod optional/null type #308

Closed

Description

@peniqliotuv

I'd love to use this library but since we redefine inputMaybeValue: 'T | undefined' in the graphql-codegen configuration, it's incompatible with the .nullish() that gets added to the zod schema when using graphql-codegen-tyespcript-validation-schema.

It would be great if we could have a configuration such as zodOptionalType that allowed us to specify optional or nullish.

Activity

  1. Code-Hex commented on Apr 16, 2023

    @Code-Hex
    Owner

    @peniqliotuv Hi 👋

    I'm sorry to delay this reply.

    Could you tell me how to do this?

    we redefine inputMaybeValue: 'T | undefined' in the graphql-codegen configuration

  2. peniqliotuv commented on Apr 18, 2023

    @peniqliotuv
    Author

    Hey @Code-Hex ! No worries.
    Yea - I'm looking for an ability to customize the nullish values generated by graphql-codegen-typescript-validation-schema.
    An example API that would be nice:

    ...
        config:
          maybeSchemaValue: 'nullish' | 'null' | 'undefined'
    

    This value would default to nullish.

    The behavior of nullish would be akin to what's currently in the plugin today.

    z.nullish()
    

    Providing null

    z.null()
    

    Providing undefined

    z.optional()
    

    I omitted the yup equivalents for simplicity's sake, but hopefully this makes it clearer!

  3. almereyda commented on Nov 26, 2023

    @almereyda

    I think instead of providing null, this should be nullable > .nullable().

    Also to provide undefined, one needs to write optional, so that renders to .optional().

  4. niklasholm commented on Nov 1, 2024

    @niklasholm

    I'm also having this issue, my current workaround is to run sed -i 's/\.nullish()/.optional()/g' in afterAllFileWrite but it feels very hacky and prone to errors.

  5. Code-Hex commented on Apr 25, 2026

    @Code-Hex
    Owner

    Fixed in #1423. Zod optional/null handling is now configurable through zodOptionalType, with nullishBehavior accepted as an alias.

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions