Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
akrynski-1 opened this issue Nov 6, 2024 · 2 comments
Closed

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

akrynski-1 opened this issue Nov 6, 2024 · 2 comments
Assignees
Labels
question Further information is requested

Comments

@akrynski-1
Copy link

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
Copy link
Owner

fabian-hiller commented Nov 6, 2024

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.

@fabian-hiller fabian-hiller self-assigned this Nov 6, 2024
@fabian-hiller fabian-hiller added the question Further information is requested label Nov 6, 2024
@akrynski-1
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants