valibot
valibot copied to clipboard
feat: create coercion actions for primitives (and date)
Decisions left:
- should we try/catch around conversions? it's rare but some can throw, like
Number(Symbol()) - v.toNumber - should we check for NaN after converting, or recommend piping into v.number?
- v.toDate - similarly, should we check the date is valid, or recommend piping into v.date?
The latest updates on your projects. Learn more about Vercel for Git ↗︎
| Name | Status | Preview | Comments | Updated (UTC) |
|---|---|---|---|---|
| valibot | ✅ Ready (Inspect) | Visit Preview | 💬 Add feedback | Jul 16, 2025 6:43pm |
Thank you so much!
should we try/catch around conversions? it's rare but some can throw, like Number(Symbol())
Yes, we should do that whenever it is necessary to cover all edge cases
v.toNumber - should we check for NaN after converting, or recommend piping into v.number? v.toDate - similarly, should we check the date is valid, or recommend piping into v.date?
I would only convert it. Do you know what Zod is doing here?
with Zod the coercion isn't separate from the schema (it just happens as the first step of the schema if configured), so z.coerce.number() would be the same as v.pipe(v.unknown(), v.toNumber(), v.number())
I will start working on this PR to get it over the line
v.toNumber - should we check for NaN after converting, or recommend piping into v.number? v.toDate - similarly, should we check the date is valid, or recommend piping into v.date?
I decided to return an issue for both as I think that this is the happy path for most users.
Feel free to review my changes.
I will finalize and merge this PR in the next days
changes look good, thank you 😄
v1.2.0 is available