Open spigelli opened 2 years ago
I like this idea a lot, I feel like it would play nicely with this RFC: https://github.com/edgedb/edgedb-js/issues/361. In fact Zod is mentioned in the RFC.
This is not just wanted, but actually REQUIRED
One tricky bit here is not depending on Zod itself and having it be a peerDependency and tracking changes to Zod here: changes to Zod's API would have potentially breaking changes to the generator. Definitely doable and I see a lot of value in being able to map EdgeDB types -> Zod. The other way around is pretty lossy, so we'll need to define exactly what the supported use cases and user flows we would want are.
Probably some prior art with similar projects we can lean into and learn from here.
+1 Hoping to have generated constraints/validations that are consistent with EdgeDB, which can then be used by Zod, Typebox and other tooling.
There is a zod schema generator for prisma.
I find it useful for form validation that works both in the client and server. Of course the server will have extra validation.
But there are many open questions:
/// @zod.import(["import Buffer from 'buffer'"])
model MyModel {
myField String /// @zod.string.min(3, { message: "min error" }).max(10, { message: "max error" }).[...chain more validators]
}
The prisma generator throws when there is an error, which is nice.
One possible way is to implement the same things using annotations ... But I'm not really fond of it, as it will have the same cons. What I would like to do is to actually implement form validation at the schema level using edgeql and be able to generate a client side form validator in wasm that I could use in the client (browser, mobile ...). In the server side, I'd lean on EdgeDB to do validation natively.
This is probably NOT possible since some EdgeDB standard functions are defined in SQL. But it should be possible to do with an extension, especially if there is a wasm extension that enables running wasm in edgeql.
I think it's important for the db and the schema to ACTUALLY be the source of truth. And data/form validation are part of that "truth"
The ability to opt in to zod schema generation would be awesome not sure how
$infer
works behind the scenes (if it has runtime type understanding)I'm wanting this for trpc but it has a lot of other benefits (bit more validation than just a type and others I'm sure)
the trpc use-case: