GithubHelp home page GithubHelp logo

Comments (5)

tomlaws avatar tomlaws commented on May 19, 2024 2

neater alternative

const schema = z.object({
  limit: z.string().pipe(z.coerce.number().min(1).max(20)).optional(),
});

from middleware.

yusukebe avatar yusukebe commented on May 19, 2024

Hi @ryoppippi,

Does the error disappear if you change the line to the following?

const schema = z.object({ userID: z.number().int() })

to

const schema = z.object({ userID: z.string() })

from middleware.

zengsun avatar zengsun commented on May 19, 2024

me too!

import { Hono } from "hono/mod.ts";
import { z } from "zod/mod.ts";
// import { validator } from "hono/validator/index.ts";
import { zValidator } from "npm:@hono/zod-validator";

const router = new Hono();
const schema = z.object({
  name: z.string(),
  age: z.number(),
});

router.post(
  "/register",
  zValidator("form", schema),
  async (c) => {
    const data = await c.req.parseBody()
    return c.json({ hello: "world", data });
  }
);

export const registerRouter = (app: Hono) => app.route("/", router);

ts error:

No overload matches this call.
The last overload gave the following error.
Argument of type 'MiddlewareHandler<Env, string, any>' is not assignable to parameter of type 'H<Env, "/register", {}, {}>'.
Type 'MiddlewareHandler<Env, string, any>' is not assignable to type 'Handler<Env, "/register", {}, {}>'.
Types of parameters 'c' and 'c' are incompatible.
Type 'Context<Env, "/register", {}>' is not assignable to type 'Context<Env, string, any>'.
Types have separate declarations of a private property '_req'.

from middleware.

yusukebe avatar yusukebe commented on May 19, 2024

@zengsun

Fix like the following:

const schema = z.object({
  name: z.string(),
  age: z.number(),
});

// To

const schema = z.object({
  name: z.string(),
  age: z.string(),
});

Because the "form" values should not be number.

from middleware.

ryoppippi avatar ryoppippi commented on May 19, 2024

hi @yusukebe !!
Thanks!
I changed like below and it works fine!!! :

const schema = z.object({
  name: z.string(),
  age: z.string().transform(v=>parseInt(v)).refine(v=>!isNaN(v), {message: 'not a number'})
});

from middleware.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.