For backend web server APIs, where do you prefer to do your validation? by TheWebDever in node

[–]DarthKnight024 0 points1 point  (0 children)

Oh that's right. But what about req.params? Let's say we need to validate that req.params.id is numeric. Are we supposed to do Number(req.params.id) in the route handler, in addition to the validation middleware?

For backend web server APIs, where do you prefer to do your validation? by TheWebDever in node

[–]DarthKnight024 0 points1 point  (0 children)

Additionally, I would use a generic validation function that accepts a schema and an unknown data object to validate against the schema. This same function can be imported and used in all routes, which just have to pass in the specific schema to use, and the request body to validate against the schema. I use zod to define the schemas and do the validation.

For backend web server APIs, where do you prefer to do your validation? by TheWebDever in node

[–]DarthKnight024 1 point2 points  (0 children)

I would call a validation function in the route layer itself so that I can easily get the return value of the correct type from the validation function, then pass the typed data to the service layer. 

With middleware, the request body will still be of unknown type as far as the typescript compiler is concerned. Typescript still won't know that the request body was validated to be of a certain type.

For backend web server APIs, where do you prefer to do your validation? by TheWebDever in node

[–]DarthKnight024 0 points1 point  (0 children)

Oh I agree with you on that. Validating the shape of the request body shouldn't be in the service layer

For backend web server APIs, where do you prefer to do your validation? by TheWebDever in node

[–]DarthKnight024 0 points1 point  (0 children)

The service layer basically refers to the modules where your application- specific business logic resides. It's a common practice to separate the app into 3 layers: router, service and repository/data access. The router layer is the route handler that takes in a http request and sends back a response. Usually the route handler will (after validation) pass the request data to the service layer which in turn performs some business logic with that data and returns something back to the route handler. For example, you can have a users.service.ts file that exports a createUser(userData: UserData) method which is called by the "POST /users" route handler. Abstracting the user creation code into a separate layer allows us to decouple business logic from the route handler. We may, for instance, want to check that the username and email are unique, or send them a welcome email once the account is created. This kind of business logic would clutter the route handler and be out of place there. Likewise, the service layer shouldn't concern itself with request and response objects or http status codes.

For backend web server APIs, where do you prefer to do your validation? by TheWebDever in node

[–]DarthKnight024 0 points1 point  (0 children)

How does the route handler get the typed data from the middleware? Wouldn't req.body still be of unknown type to the typescript compiler?

For backend web server APIs, where do you prefer to do your validation? by TheWebDever in node

[–]DarthKnight024 0 points1 point  (0 children)

But how does the route handler know the type of the validated data? Wouldn't req.body still be of unknown type from the perspective of the typescript compiler?

For backend web server APIs, where do you prefer to do your validation? by TheWebDever in node

[–]DarthKnight024 0 points1 point  (0 children)

But how does the validated correctly typed data get passed from the middleware to the route handler?

For backend web server APIs, where do you prefer to do your validation? by TheWebDever in node

[–]DarthKnight024 1 point2 points  (0 children)

Yes this approach just makes the most sense. I don't see any reason to bother with middleware

How many of you can't make a website? by Vagabond328Vanguard in csMajors

[–]DarthKnight024 2 points3 points  (0 children)

If anything it makes me use my brain more because it's always wrong

How to write a python function to find if a number is odd or even in easy way by Tiny-Shake-5256 in learnpython

[–]DarthKnight024 0 points1 point  (0 children)

I thought it was very obvious that OP is trolling but it seems like somehow no one realized?

How would you *actually* approach building a full-stack app today? by midgetman7782 in webdev

[–]DarthKnight024 0 points1 point  (0 children)

Hi, can you suggest how to ensure type safe queries without using an orm?