Replies: 5 comments 3 replies
-
|
Hi @nateiler thanks for opening this discussion. @svozza we discussed the stated limitation/assumption so I agree with the assessment above irw the fact that it's not a bug in itself however I'm not sure about this:
What do you think? At first look I think mutating should be supported, but I'd like to hear your opinion on whether there are tradeoffs I'm missing. |
Beta Was this translation helpful? Give feedback.
-
|
Thanks @dreamorosi, @svozza I suppose there are a couple paths forward:
@rcaughtlaf - and feedback on your end? This should apply to both the body and header response validations |
Beta Was this translation helpful? Give feedback.
-
|
Apologies folks, I've been on PTO the past week and only catching up now. Imo, I don't think it is the responsibility of a validation layer to mutate the data that it is assessing so I would would most be amenable to option 1) as outlined by @nateiler. I am, however, willing to listen to strong arguments or prior art here to change my mind but I can't think of any other web framework that will change your responses in this way. I agree that runtime response validation is somewhat niche, for me the best way to handle responses is to simply use return types and get compilation guarantees given that, unlike requests, responses are controlled by the server: const createTodoSchema = z.object({ title: z.string() });
type Todo = { id: string; title: string; completed: boolean };
app.post(
'/todos',
(reqCtx): Todo => {
const { title } = reqCtx.valid.req.body;
return { id: '123', title, completed: false };
},
{
validation: { req: { body: createTodoSchema } },
}
); |
Beta Was this translation helpful? Give feedback.
-
IMO, this a Standard Schema layer, not just a validation layer. It would be confusing that only a subset of Standard Schema functionality gets "supported".
A couple of things:
For requests and responses, I personally think that anything under the |
Beta Was this translation helpful? Give feedback.
-
These are very compelling arguments that have convinced me to change my mind. Are we saying then that if a user is using request validation that the response that goes to the client is what's in |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
First, @svozza thank you for all your work in the addition to req/res validation!
I briefly brought this up while working on this PR, but the schema response validation doesn't seem to play nicely w/ schemas that coerce the result.
Let's take this schema:
I would expect if it were used as the response body validation schema that the results would reflect the schema. For example:
This test fails because the validation doesn't actually mutate the response. At the moment, is only way to use the parsed/validated response is with another middleware that does the mutation?
I don't think this is a bug as the docs clearly state the intent "[Validated data is available via reqCtx.valid.res and is fully typed based on your schema.](Validated data is available via reqCtx.valid.res and is fully typed based on your schema.)" however I personally find response validation useless as the majority of schemas either coerce (a Date object to string) or omit internal attributes not meant for the response payload.
Beta Was this translation helpful? Give feedback.
All reactions