Uncaught TypeError: Cannot convert a Symbol value to a number
See original GitHub issueThe following code sample will throw said error because after the validation failed the refine
function is still called:
z.union([z.number(), z.string().transform(z.number(), Number.parseFloat)])
.refine((v) => v >= 1)
.parse("foo");
This is supposed to accept 5
or "5"
and reject anything that is not parsable into a positive number.
But the refine will be called even if the union parsing failed and then v
will be Symbol("invalid_data")
> z.union([z.number(), z.string().transform(z.number(), Number.parseFloat)]).refine((v) => v >= 1).parse("foo");
Uncaught TypeError: Cannot convert a Symbol value to a number
at repl:1:92
at Object.check (/home/despairblue/git/wunderflats/api/node_modules/zod/lib/cjs/types/base.js:167:34)
at ZodUnion.parse (/home/despairblue/git/wunderflats/api/node_modules/zod/lib/cjs/parser.js:892:41)
>
Without the refine it works as expected:
> z.union([z.number(), z.string().transform(z.number(), Number.parseFloat)]).parse("foo");
Uncaught ZodError: [
{
"code": "invalid_union",
"unionErrors": [
{
"issues": [
{
"code": "invalid_type",
"expected": "number",
"received": "string",
"path": [],
"message": "Expected number, received string"
}
]
},
{
"issues": [
{
"code": "invalid_type",
"expected": "number",
"received": "nan",
"path": [],
"message": "Expected number, received nan"
}
]
}
],
"path": [],
"message": "Invalid input"
}
]
at new ZodError (/home/despairblue/git/wunderflats/api/node_modules/zod/lib/cjs/ZodError.js:73:28)
at ZodUnion.parse (/home/despairblue/git/wunderflats/api/node_modules/zod/lib/cjs/parser.js:195:17) {
issues: [
{
code: 'invalid_union',
unionErrors: [Array],
path: [],
message: 'Invalid input'
}
],
addIssue: [Function],
addIssues: [Function],
flatten: [Function]
}
>
Question regarding this. It seem refine
function are not allowed to throw. Does that mean a refine
function must succeed?
Forget the last part as they can return false
obviously.
Issue Analytics
- State:
- Created 3 years ago
- Comments:9 (9 by maintainers)
Top Results From Across the Web
Why Symbols not convert string implicitly - Stack Overflow
I think OP is asking why JavaScript gives an error rather than converting the Symbol to a string automatically like it does with...
Read more >TypeError: Cannot convert a Symbol value to a string ... - GitHub
I receive the following error: TypeError: Cannot convert a Symbol value to a string at b (/code/node_modules/mobx/dist/mobx.cjs.production.
Read more >Cannot convert a Symbol value to a string - Questions
The general issue with the Symbol was mentioned here [VUE] TypeError: Cannot convert a Symbol value to a string and solved for Vue...
Read more >Symbols • JavaScript for impatient programmers (ES2022 ...
Symbols are primitive values that are created via the factory function ... 'Symbol I used: ' + mySymbol TypeError: Cannot convert a Symbol...
Read more >Receiving error "Cannot convert a Symbol value to a string" at ...
This kind of error occurs when you are attempting to coerce a Symbol into a String : jsfiddle.net/Lt9qn2r5. – pmdartus. Sep 2, 2020...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks for all your work on this! I merged these last night 🤙
I created 2 PRs, one for the bug and one for making it clear that refinements and transformers must not throw.
Makes sense, but I’ll probably just continue using it like this. The reason is that needing a refinement and a transformer to be kept in sync (which may also be defined in totally different places) is a bug that is just waiting to happen. Technically it’s a clean separation of concerns, but the usability for the developer/team using this is hurt by this I fear.
Regarding 3. I currently went with defining all refinements and transformations in another file and wrap them in a try catch function, the refinement one returns just false on error and the transformer the value that was passed in. Seems to work so far.