You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This may seem strange, but it's because I've been dealing with AWS CloudFormation parameters, where you can only have empty strings, not null or undefined values...
I was trying to do
// helper used in place of `.optional()` for my schema for parsing CloudFormation metadatafunctionoptional<Textendsz.ZodTypeAny>(schema: T){returnz.union([z.literal('').transform(()=>undefined),schema.optional()]).optional()}constparameter=optional(z.string()).default('test')
I expected parameter.parse('') to be 'test' but instead it's undefined. This violates the output type of .default('test'), so I think even if the input is defined, ZodDefault should check that the parse output is too.
The text was updated successfully, but these errors were encountered:
jedwards1211
changed the title
Should .default() parse with the wrapped schema and then check if the parsed value is undefined?
.default() should apply default if wrapped schema's parse returns undefined
Jul 3, 2024
jedwards1211
changed the title
.default() should apply default if wrapped schema's parse returns undefined
.default() should apply default if wrapped schema transforms to undefined
Jul 3, 2024
Here we're seeing the consequence of designing ZodDefault to change the input to a default value if it's undefined, rather than just changing the output value if it's undefined.
To guarantee that ZodDefault does produce the correct output type, I had to:
Re-parse on the default value if parsing a non-null value output undefined
In the worst case, even parsing the default value outputs undefined -- I had to add an error for this case
This may seem strange, but it's because I've been dealing with AWS CloudFormation parameters, where you can only have empty strings, not null or undefined values...
I was trying to do
I expected
parameter.parse('')
to be'test'
but instead it'sundefined
. This violates the output type of.default('test')
, so I think even if the input is defined,ZodDefault
should check that the parse output is too.The text was updated successfully, but these errors were encountered: