Coerce bigint throws TypeError
instead of ZodError
#1856
-
Consider the following code: var zod = require("zod")
const testSchema = (schema, invalidData) => {
try {
schema.parse(invalidData);
} catch (e) {
if (e instanceof zod.ZodError) {
console.log("throws ZodError");
} else {
console.log(`throws other error (${e.message})`);
}
}
}
console.log("case 1");
testSchema(zod.object({
foo: zod.string(),
}), {}); // throws ZodError
console.log("case 2");
testSchema(zod.object({
foo: zod.coerce.number()
}), {
foo: "invalid"
}); // throws ZodError
console.log("case 3");
testSchema(zod.object({
foo: zod.coerce.bigint(),
}), {}); // throws other error (Cannot convert undefined to a BigInt)
console.log("case 4");
testSchema(zod.object({
foo: zod.coerce.bigint(),
}), { foo: "invalid" }); // throws other error (Cannot convert invalid to a BigInt) (Playground: https://runkit.com/embed/bk8jnwfz1tkq) I would expect that all cases would throw a Tested with zod This is a bug, right? |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 8 replies
-
Everything seems to be working fine to me. const schema = z.coerce.bigint()
console.log( schema.parse( 42 ) ) // 42n
console.log( BigInt( 42 ) ) // 42n
try {
schema.parse( undefined )
} catch ( error ) {
console.error( error )
// TypeError: Cannot convert undefined to a BigInt
}
try {
BigInt( undefined )
} catch ( error ) {
console.error( error )
// TypeError: Cannot convert undefined to a BigInt
} |
Beta Was this translation helpful? Give feedback.
-
I might misunderstand the usage for "coerce", I'm using zod for validating the input in a REST endpoint. If the user forgets to send a value in an object (which I'm planning to check against my database which has id:s of type |
Beta Was this translation helpful? Give feedback.
-
perhaps the docs about coerce would help. Does this work for you? const schema = z.object( {
id: z.coerce.bigint().optional(),
} )
console.log( schema.safeParse( { id: 42 } ) )
// { success: true, data: { id: 42n } }
console.log( schema.safeParse( { id: '42' } ) )
// { success: true, data: { id: 42n } }
console.log( schema.safeParse( {} ) )
// { success: true, data: {} } |
Beta Was this translation helpful? Give feedback.
-
How about this? const schema = z.object( {
id: z.any().transform( ( value, ctx ) => {
try {
return BigInt( value )
} catch ( error ) {
ctx.addIssue( {
code: 'invalid_type',
expected: 'unknown',
received: value,
message: `Can't be parsed to BigInt`,
} )
}
} ),
} )
console.log( schema.safeParse( { id: 42 } ).success ) // true
console.log( schema.safeParse( { id: '42' } ).success ) // true
console.log( schema.safeParse( { id: 'invalid' } ).success ) // false
console.log( schema.safeParse( {} ).success ) // false |
Beta Was this translation helpful? Give feedback.
-
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 schema = z.object( {
foo: zu.coerce( z.bigint() ),
} )
console.log( zu.SPR( schema.safeParse( {} ) ).error?.issues )
// [
// {
// code: 'invalid_type',
// expected: 'bigint',
// received: 'undefined',
// path: [ 'foo' ],
// message: 'Required'
// }
// ]
console.log( zu.SPR( schema.safeParse( { foo: 'invalid' } ) ).error?.issues )
// [
// {
// code: 'invalid_type',
// expected: 'bigint',
// received: 'string',
// path: [ 'foo' ],
// message: 'Expected bigint, received string'
// }
// ] |
Beta Was this translation helpful? Give feedback.
How about this?