nightspite
nightspite6mo ago

How do I exclude properties while returning data?

I basically need to create a dto's that would exclude properties like access keys etc. while returning the data to the client - so basically just striping the object on return. How can that be achieved?
4 Replies
BeBoRE
BeBoRE6mo ago
You can use Zod schema’s for that, you can also add them as output schemas to make sure you aren’t returning something that you shouldn’t be
nightspite
nightspite6mo ago
how do you actually achieve that? you can technically parse them, so they will automatically strip all unnecessary properties, but if the parse would fail, then you are left with zod validation error and nothing got returned
BeBoRE
BeBoRE5mo ago
That’s the point right, you wouldn’t want to send that data if it wasn’t what you’d expect the output to be?
nightspite
nightspite5mo ago
Sure in most of the cases, but in the edge case, where for example a value of one property is null I would rather have it returned with the null there than to have a validation error. Or in the case of using the same schema for validating input (for example setting min length of a string) and output, if a record with min length that would not satisfy this validation would also throw the error, even tho it might be unnecessary. Just my way of thinking about this problem. I had this problem with ts-rest, that is also using zod for validating output, that's why it's on my mind and I'm looking for other ways to solve it