-
-
Notifications
You must be signed in to change notification settings - Fork 244
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
typia/zod support #20
Comments
I have this consideration I have previously with Zod/io-ts/typia. For one, I want to put out as fast as possible while still balancing with Quality of Life, Typia introduces two mature problems that make me not support Typia (at least for now).
And in regard to performance, TypeBox is using AoT compilation to improve performance SummaryWe may support Typia someday or might not support Typia at all (depending on the situation that may unfold in the future), but for now, we have no interest or priority to support Typia at the moment, and if we have to support 2nd validation library one day, it's likely that Zod will be our priority next. I will leave this issue open for discussion, feel free to ask more questions if the answer is not clear. |
very satisfying answer, thanks @SaltyAom ! I also wasn't thrilled with the ttsc business and very much enjoyed my approach to typebox and zod. thanks for all your work and generosity! |
Whilst Typebox for OpenAPI support is a nice 'feature', these days in terms full stack javascript development (elysia is node.js) it's not always needed given the focus on server actions which use typescript types, so adding validation to exsting types using typia I feel is a nice solution. I don't really see that big a difference DX wise from Typebox to Zod. |
@SaltyAom I've found the interesting repo: https://github.com/decs/typeschema for Universal adapter for TypeScript schema validation. They support various type of data validation. It might be help. |
FWIW Zod type-checking performance has historically been pretty poor as projects increase in complexity. However, I have not checked on a real-world project lately. Most recent deep dive I've found -> https://dev.to/nicklucas/typescript-runtime-validators-and-dx-a-type-checking-performance-analysis-of-zodsuperstructyuptypebox-5416 |
Unfortunately, there is a difference in DX... The TypeBox does nothing more than the JSON SCHEMA allows itself to do By the way, there is an interesting alternative to Zod - Valibot (Site, Github)
It's impressive! But we need to make sure that we don't lose anything from integrating this t.Object({
avatar: t.File({
type: "image",
}),
}), I'm not ready to give it up! It's too convenient |
Two things that "jump out" at me are:
const NonEmptyString = z.string().min(1).brand("NonEmptyString");
type NonEmptyString = z.infer<typeof NonEmptyString>;
const greet = (s: NonEmptyString) => z.string().min(1); This sort of approach is handy for protecting your system from invalid data at the type level.
|
Saw the trpc plugin and decided to give elysia a go, sure enough spotted the "compile" import in the example only after hitting a type error passing zod schemas to the procedures. Definitely a non-starter, would have been an instant switch from fastify otherwise. Even for a greenfield project like the one I wanted to try elysia for, many folks already have their big bag of zod tools and patterns they would like to slingshot a new project with. For example https://github.com/chrishoermann/zod-prisma-types is alone responsible for dozens if not hundreds of hours saved during prototyping phase (not to mention react-hook-form and its zodResolver on the other side). The switch over to a proprietary validator is a tall ask and definitely stunts this project. Sorry, I know how this probably reads, but I've a good feeling that many people are not bothering to say anything and instead have just gone to use something else. |
@medv 's sentiment:
This is an anecdotal scenario of why ubiquity can be meaningful.
To their point... what's stopping this from being an extensible interface? (besides the obnoxiousness that might come from some additional generic typing) |
https://github.com/turkerdev/fastify-type-provider-zod Can this be of any help? |
https://typia.io/docs/setup/#unplugin-typia Typia can now be used in Next.js and Bun environments. |
Closing as not planned at the moment, however feels free to continue to discussion or open a new discussion in the discussion tab. Thanks. |
@SaltyAom the reason I'm keen for Zod support, is being able to do cross-field validation. Is there any possible way to do the following in Elysia atm? t.Object({
max: t.Number(),
min: t.Number({ minimum: 0 }),
}) Given this TypeBuilder, validate that |
Ahh I just saw beforeHandle in the docs 👍🏼 |
Here it remains only to use the type box or write your own validator/serializer, which will be completely covered by types and JsonSchema last draft(2020-12) + OpenAPI 3.1.0 support. For example, python has a wonderful pedantic library, you can be inspired by it, their main core is written in rust for data serialization. You can also write a kernel on growth and compile it into wasm |
In a framework that works like magic, it's sad to write validations manually. It's even sadder that by far the most upvoted issue is marked as wontfix. |
I can't say anything about other reasons, you're probably right, but I should specify that Typia has a JSON Schema generation feature: https://typia.io/docs/llm/schema/ |
The creator of Zod has proposed a Standard Schema API for libraries to support instead of a single validation library. List of current libraries consuming it: https://github.com/standard-schema/standard-schema?tab=readme-ov-file#ecosystem |
Seems interesting |
Issues like this one make me feel like TypeBox is just not at all designed for UX, instead it focuses solely on performance whilst checking and parsing data. Most use cases for me and anyone working on web apps will be form validation with custom error messages and i18n. I really think that Zod should be considered for a project like Elysia, since it's mature and well designed for custom error messages and form validation. Looks like someone worked on a branch to try this out https://github.com/Savolus/elysia-with-zod |
Typebox designed for perfomance and JSON Schema Builder I guess our community can create new one validator that aims DX and perfomance a lot of things of DX can be JIT'ed to improve perfomance |
need this! |
I really tried translating my Zod schemas to TypeBox because of the general performance benefits Elysia offers. But man, I couldn’t pull it off. I thought I’d install the main TypeBox module and start from there. Then I realized I couldn’t use that official TypeBox module but had to use Elysia.t. Is that correct? I can’t justify making my entire project dependent on a single web framework, especially a brand new one. The coupling is too much, which increases the risk. While coupling Elysia to TypeBox is theoretically sound with legitimate reasoning, it’s not very pragmatic; I hope a Zod to Elysia.t adapter becomes available at some point. Also, why wrap TypeBox? Why not have an adapter that makes existing TypeBox schemas Elysia compatible? I love the work done here; there’s lots of great stuff. I’ll keep an eye on how it progresses. Definitely in the future, starting fresh, I’ll give it a try, especially since I’m using Bun for all of my Node projects nowadays. |
|
Try passing in a raw TypeBox schema not built with t. |
it is because elysia uses -1 major of typebox) |
I'm...not following? My point was that I would need to |
Just import from “@sinclair/typebox” package without installing anything. It should use Elysia version of Typebox. @enriquecaballero |
I tried that... |
Don’t install @sinclair/typebox package too. I’m using this way and works. |
I would rather use @sinclair/typebox as the dependency in my code and then use the schemas defined with @sinclair/typebox with Elysia. NOT the other way around. |
Override typebox version in elysia |
… Can you elaborate be more specific? As I said in my original comment, I tried to make this work and I tried various things. It’s not so trivial. |
@enriquecaballero-agx @enriquecaballero @kravetsone @jasperdunn @rjzheng
A new TypeBox library is available that enables the integration of Zod and Valibot into Elysia. This library works by transforming Zod and Valibot types into TypeBox-compatible schematics (i.e., JSON Schema). It allows Zod and Valibot validation to be accelerated by the TypeBox compiler and makes these libraries compatible with OpenAPI/Swagger. Project: https://github.com/sinclairzx81/typebox-adapter ExampleThe following creates an Elysia route and configures parameters using TypeBox, Zod and Valibot. Type inference is preserved. import { Box } from '@sinclair/typebox-adapter'
import { Elysia, t } from 'elysia'
import z from 'zod'
import v from 'valibot'
new Elysia()
.get('/remix', ({ params, query, body }) => {
const { x, y, z } = params // from TypeBox
const { a, b, c } = query // from Zod
const { s, t, u } = body // from Valibot
}, {
params: t.Object({
x: t.String(),
y: t.Number(),
z: t.Boolean()
}),
query: Box(z.object({
a: z.string(),
b: z.number(),
c: z.boolean()
})),
body: Box(v.object({
s: v.string(),
t: v.number(),
u: v.boolean()
})),
})
.listen(3000) This project is brand new, with work continuing through 2025. I anticipate additional updates will be required when Zod 4 is released, and there are considerations to include other libraries like EffectTS in the future. If you use this project, feel free to submit issues or provide general feedback. Hope this helps |
@sinclairzx81 Do you think there will be a chance to add Typia support in the future? |
typia support is hard because it AOT with builder plugin |
Fine! But i guess remix not so good name because react-remix)) also did u want to work on new validation library? Like Typebox but with more DX and JITed Like Zod but really faster (for example compile string().maxLength(8, "error").trim() to a function) JSON schema needed but can be generated by remappers from internal |
I’m currently converting schema() function output (which is JSON schema) to Typebox schema. But the lack of first-party support bothers me. But it works. |
@kravetsone Hi,
I agree. Remix was the working title used during development, but I guess it's not as descriptive as it could be. I have republished under a the more formal name github: https://github.com/sinclairzx81/typebox-adapter Thanks for the feedback :) |
Great! |
any chance?
The text was updated successfully, but these errors were encountered: