Code Monkey home page Code Monkey logo

Comments (6)

lanwin avatar lanwin commented on June 6, 2024 1

Well, if you develop an web application with typescript you always have do deal with the problem that input parameters and form parameters are untyped strings. You always have to write code to convert them to an typed object. Even in simple cases like simple paramters like number page and number size.

The most other frameworks give you a simple object with properties of type string and I have written a lot code that handles the conversion to proper types in the past.

So from my perspective a web framework with first class typescript support, should offer a build in way to deal with that kind of problems.

from tinyhttp.

aarontravass avatar aarontravass commented on June 6, 2024

You can probably just do

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

app.get('/user', (req, res) => {
    const user =  userSchema.parse(req.query)
    ....
`);

In my opinion, the validate is just redundant

from tinyhttp.

lanwin avatar lanwin commented on June 6, 2024

The problem here is that this would throw an error if schema validation fail. With would result into an 500er.

To mitigate that I would need to add an try catch to every handler with res.status(400).send("validation failed"). That would be pretty verbose. Or add an general handler that catches the zod error and returns bad request. But that code must also be copy and pasted to each project you would want to use.

But even with that, the context gets lost. We have an zod validation error and did not know where it come from. Did the error was userSchema.parse(req.query) or userSchema.parse(req.body).....

With validate.query(req, userSchema) we have the context and can raise a proper error. 400 query parameters arent valid....

from tinyhttp.

aarontravass avatar aarontravass commented on June 6, 2024

I understand. Thank you for the explanation. My only gripe is that this would not be validation agnostic but in hindsight it's ok since nestjs also uses class validator in their docs.

@talentlessguy what do you think?

from tinyhttp.

talentlessguy avatar talentlessguy commented on June 6, 2024

is there something similar for other web framework?

Implementing a custom middleware should be fairly straightforward

IMO there's no need for an official one

from tinyhttp.

talentlessguy avatar talentlessguy commented on June 6, 2024

fair points but I think a userland package would be sufficient. There are other validator libraries and integrating and keeping up with each single library is a lot of labor.

I'm not opposed to adding a link to any tinyhttp-focused validator libraries to the website and docs.

from tinyhttp.

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.