admin管理员组文章数量:1126318
I need to validate an object with a field that may be bigint, number, or undefined.
const IdNumberSchemaNullable = z.union([
z.number().nonnegative().optional(), // For floating-point numbers and integers
z.bigint().nonnegative(), // For big integers (BigInt type)
z.undefined()
]);
const OrderDetailsSchema = BaseSchema.extend({
_t: z.literal('my_something'),
id_test: z.IdNumberSchemaNullable,
All works, except when an object does not contain id_test
field - I get following exception:
TypeError: Cannot read properties of undefined (reading '_parse')
at ZodObject._parse (file:////node_modules/zod/lib/index.mjs:2526:37)
at ZodObject._parseSync (file:////node_modules/zod/lib/index.mjs:649:29)
at ZodDiscriminatedUnion._parse (file:////node_modules/zod/lib/index.mjs:3010:27)
at ZodDiscriminatedUnion._parseSync (file:////node_modules/zod/lib/index.mjs:649:29)
at file:////node_modules/zod/lib/index.mjs:2373:29
at Array.map (<anonymous>)
at ZodArray._parse (file:////node_modules/zod/lib/index.mjs:2372:38)
at ZodArray._parseSync (file:////node_modules/zod/lib/index.mjs:649:29)
at ZodArray.safeParse (file:////node_modules/zod/lib/index.mjs:679:29)
at ZodArray.parse (file:////node_modules/zod/lib/index.mjs:660:29)
本文标签: javascriptHow to express bigint or number or undefined using Zod SchemaStack Overflow
版权声明:本文标题:javascript - How to express bigint or number or undefined using Zod Schema - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736684807a1947605.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论