Skip to content

[Help/Question] Function with optional parameters, ZodError: "too_small" #949

Discussion options

You must be logged in to vote

Explanation

optional in zod doesn't mean optional the way you might think. All it means is that the value can be undefined. undefined doesn't always mean non-existent. It is a little confusing, yes.

Most of the time, optional works the same as how you would expect, but in the case of function args or tuples, saying that it's optional just means that it needs to exist, but it can be undefined.

This is essentially what you are doing:

const args = z.tuple( [
    z.number(),
    z.string().optional(),
    z.string().optional(),
] )

console.log( args.safeParse( [ 42, 'CAD', 'en-CA' ] ).success ) // true
console.log( args.safeParse( [ 42, undefined, undefined ] ).success ) // true
console.log( a…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@kristremblay
Comment options

Answer selected by kristremblay
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #942 on February 20, 2022 00:31.