Skip to content

Conversation

@benjaminstrasser
Copy link
Member

This pull request explores the feasibility of using Zod schemas as the foundation for our database type definitions. The primary motivation behind this approach is to leverage the numerous benefits that Zod offers for type validation and type safety. While this PR is very much a draft and not up to our usual code quality standards, it serves as a proof of concept and a starting point for discussion. I was pretty much just interested if it works.

This would allow us to see the values returned from our db to be what they are - potentially unsafe. This is especially true as kysely does not validate what is returned from our database, but very much suggests that the values conform to our types. This was briefly discussed with @jjnp.

@benjaminstrasser benjaminstrasser requested a review from mledl as a code owner June 14, 2024 14:02
@benjaminstrasser benjaminstrasser marked this pull request as draft June 14, 2024 14:02
Signed-off-by: Benjamin Strasser <bp.strasser@gmail.com>
@sjaghori
Copy link
Collaborator

sjaghori commented Jun 15, 2024

I think we can treat the DB as an internal component. We can ensure data integrity through testing.
I really like the idea of validation with Zod because it's convenient and effective. However, I suggest we continue validating the endpoints (client-to-server communication) and not the server-to-database communication, as it's an additional step in the process of creating a simple database query.
Another point against validating server <-> database communication is the runtime overhead (we want to be blazingly fast).

I'll reconsider my decision when we release the project and hit over 1k stars on GitHub. :)

@mledl
Copy link
Contributor

mledl commented Aug 31, 2024

In general this is not a bad idea. However, the main points of validation should be on the client and when data is passed to our internal API (if there is any). In many cases, the data model for passing data to service layer in order to be persisted differs from the actual DB schema. Therefore, I do not really see the big advantage of this generation.
I really see a big advantage when when creating service layer and client side schemas and infer models from them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants