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.

How to cast string to number?

See original GitHub issue

I have something like this:

const params = z.object({
  id: z.number()
})

This fails immediately since id is a query string param and so is a string. Then I tried:

const params = z.object({
  id: z.transformer(z.string(), z.number(), x => Number(x))
})

But the compiler yells:

Type 'ZodObject<{ id: ZodTransformer<ZodString, ZodNumber>; }, "passthrough", ZodTypeAny, { id: number; }, { id: string; }>' is not assignable to type 'ZodType<{ id: number; }, ZodTypeDef, { id: number; }>'.
  The types of '_input.id' are incompatible between these types.
    Type 'string' is not assignable to type 'number'

What am I doing wrong?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
colinhackscommented, Dec 27, 2020

There’s a lot to unpack here.

  1. ZodSchema is an alias for ZodType, they’re interchangable.
  2. Since the introduction of transformers in v2, ZodType is a generic class with three generic parameters: ZodType<Output, Def, Input>. For transformers Input and Output are different, for all other schemas they’re the same.
  3. For the sake of backwards compatibility, if you leave out Input from the generic definition (like you do with ZodType<P>) then the Input defaults to Output. You can see this here:

Screen Shot 2020-12-27 at 9 23 43 AM

Anyway, the solution is to fully type the ZodTypes in your interface. Without your full code I can’t test it myself. This should work:

interface RouteDefinition<I, O, P> {
  path: string
  method?: HttpMethod
  validate?: {
    input?: ZodType<I, any, any>
    output?: ZodType<O, any, any>
    params?: ZodType<P, any, any>
  }
  resolve(ctx: Context<I, P>): Promise<Response<O>>
}
0reactions
brielovcommented, Dec 30, 2020

@MFogleman Hi there, I’m not using Hapi, I was just making a proof of concept. The idea was to build a very simple type-safe http server from scratch. You can check it out here. I know the code sucks but the idea was to define a route specifying the body, params and return body types and return the appropiate http status code if any of the validation fails. I manage to make it work by doing ZodType<T, any, any> but I’ll check out what you specified in your comment.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Java String to Int – How to Convert a String to an Integer
1. Use Integer.parseInt() to Convert a String to an Integer · 2. Use Integer.valueOf() to Convert a String to an Integer.
Read more >
Converting strings to numbers with vanilla JavaScript
The parseInt() method converts a string into an integer (a whole number). It accepts two arguments. The first argument is the string to...
Read more >
7 ways to convert a String to Number in JavaScript
7 ways to convert a String to Number in JavaScript · 1. Using parseInt() · 2. Using Number() · 3. Using Unary Operator...
Read more >
How to convert a string to a number - C# Programming Guide
You convert a string to a number by calling the Parse or TryParse method found on numeric types ( int , long ,...
Read more >
Java Convert String to int - javatpoint
We can convert String to an int in java using Integer.parseInt() method. To convert String into Integer, we can use Integer.valueOf() method which...
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