question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Uncaught TypeError: Cannot convert a Symbol value to a number

See original GitHub issue

The 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:closed
  • Created 3 years ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
colinhackscommented, Dec 8, 2020

Thanks for all your work on this! I merged these last night 🤙

1reaction
despairbluecommented, Dec 5, 2020

I created 2 PRs, one for the bug and one for making it clear that refinements and transformers must not throw.

don’t think the “trick” you’re referring to should be documented as a valid/acceptable approach. Like you say elsewhere, you should use refinements to make sure the inputs are valid before calling .transform

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found