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

Calling isOptional() on a coerce.bigint throws an error #1911

Open
marcus13371337 opened this issue Jan 19, 2023 · 5 comments
Open

Calling isOptional() on a coerce.bigint throws an error #1911

marcus13371337 opened this issue Jan 19, 2023 · 5 comments
Labels
bug-confirmed Bug report that is confirmed enhancement New feature or request not-intuitive-behavior

Comments

@marcus13371337
Copy link
Contributor

Try the following code:

const bigintSchema = z.coerce.bigint();
const isOptional = bigintSchema.isOptional();

That code should be able to run IMO, but now it throws an error, TypeError: Cannot convert undefined to a BigInt

@marcus13371337
Copy link
Contributor Author

Kind of related to the following:
#1897
#1856

I can't really see the benefit of why Zod shouldn't catch these errors and handle them in the same way as you use to with most of the other errors that can occur. What are the drawbacks of having Zod a bit smarter and having this as the default implementation of coerce.bigint?

        try {
            return BigInt( value )
        } catch ( error ) {
            ctx.addIssue( {
                code: 'invalid_type',
                expected: 'unknown',
                received: value,
                message: `Can't be parsed to BigInt`,
            } )
        }

@JacobWeisenburger JacobWeisenburger self-assigned this Jan 19, 2023
@JacobWeisenburger
Copy link
Contributor

I added an easier way to work with coerce to my utilz library. Perhaps this will help you.

https://github.com/JacobWeisenburger/zod_utilz#coerce

import { zu } from 'zod_utilz'
const bigintSchema = zu.coerce( z.bigint() )
console.log( zu.SPR( bigintSchema.safeParse( undefined ) ).error?.issues )
// [
//     {
//       code: 'invalid_type',
//       expected: 'bigint',
//       received: 'undefined',
//       path: [],
//       message: 'Required'
//     }
// ]

const isOptional = bigintSchema.isOptional() // no ts error
console.log( isOptional ) // false, as expected

@JacobWeisenburger
Copy link
Contributor

@marcus13371337
Was my answer satisfactory? or would you like to leave this issue open as an enhancement request?

@marcus13371337
Copy link
Contributor Author

Yes, I was able to get around the problem by creating a custom bigint-schema. But I don't see why zod, shouldn't be able to reply of a schema is optional without crashing (which happens to be a coerce bigint), to me that's clearly a bug

@JacobWeisenburger JacobWeisenburger added bug-confirmed Bug report that is confirmed enhancement New feature or request labels Jan 23, 2023
@RobinTail
Copy link
Contributor

RobinTail commented Apr 18, 2023

same about isNullable

const f = z.coerce.bigint();
f.isNullable();

despite the fact that .isNullable() calls .safeParse().

I think the confusion comes from the expectation that coerce is expected to be fail safe.
However, it's not.
In general, I don't like this feature. It brings more issues than benefits.
CC @colinhacks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug-confirmed Bug report that is confirmed enhancement New feature or request not-intuitive-behavior
Projects
None yet
Development

No branches or pull requests

3 participants