fabian-hiller / valibot

The modular and type safe schema library for validating structural data 🤖
https://valibot.dev
MIT License
6.32k stars 204 forks source link

v.nullish(v.string(), undefined) alternative #913

Closed akrynski-1 closed 2 weeks ago

akrynski-1 commented 2 weeks ago

I updated valibot and discovered that after #878 the following pattern does not convert null to undefined anymore.

const schema = v.nullish(v.string(), undefined);
const result = v.parse(schema, null);
console.log(result); // null

Types also reflect that change, what is recommended way of doing this conversion now?

fabian-hiller commented 2 weeks ago

Hey, sorry for producing a bug in your code. Here is a fix:

const schema = v.nullish(v.string(), () => undefined);
const result = v.parse(schema, null);
console.log(result); // undefined

Due to some TypeScript issues, undefined is now treated as having no default. The workaround is to pass a function that returns undefined.

akrynski-1 commented 2 weeks ago

Thank you for a quick reply, fix you provided does work for me