Comments (6)
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.
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.
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.
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.
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.
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)
- Response object interface doesn't match @types/express behavior, it's not really generic HOT 6
- Simple Pull Request - License-none
- Failed to start https server HOT 1
- Multiple path arguments (as array) to route HOT 4
- Link CONTRIBUTING.md in README.md and mention forking as the first step instead of clone HOT 1
- Incompatible with Express middleware: `express.Handler` HOT 26
- Mounting a subapp on array of paths
- Add missing `accepts` TypeScript signatures HOT 4
- Mallformed URI's leads to crash with `/thing/:id` routes HOT 1
- app.use(<path>, <Router>) is Incompatible with Express HOT 8
- Exporting a router file is not working HOT 7
- Create a community server HOT 8
- The website is down HOT 7
- Request `.xhr` misses fetch requests (headers are lowercase) HOT 3
- req.route always returns undefined HOT 19
- Server instance (of `@tinyhttp/app`) hangs on test since 2.0.33 HOT 4
- Predefined error type HOT 4
- Wildcards strip the first character from `req.url` HOT 10
- [docs] outdated app.engine example with eta HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tinyhttp.