Skip to content
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

Open
gotjoshua opened this issue Feb 18, 2023 · 10 comments
Open

typia/zod support #20

gotjoshua opened this issue Feb 18, 2023 · 10 comments
Labels
good first issue Good for newcomers

Comments

@gotjoshua
Copy link

any chance?

@SaltyAom
Copy link
Member

SaltyAom commented Feb 23, 2023

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).

  1. It shouldn't only just check the type, the reason I pick TypeBox is that it also supports OpenAPI Schema which is important to make a standard production-grade REST API server, and the ability to specify server specs and generate documentation of fly.

Many have asked why Elysia uses type box instead.

  1. Typebox is built around OpenAPI which made the Swagger plugin possible.
  1. Typia requires an additional setup step and forces the developer to go TypeScript-only.

  2. The cost of adding an extra union type to support End-to-End type safety is likely very high and complex, For one to make this full type-safety work, Elysia is using depends on LocalHandler, LocalHook, ElysiaRoute TypeRouteToEden, and CreateEden which is highly a complex type consist of over 600 loc for typing only, at the moment the type inference isn't as fast as it should be but if we manage to add one more union type for Typia, it's likely to have a problem with the performance of type inference.

And in regard to performance, TypeBox is using AoT compilation to improve performance

Summary

We 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.

@SaltyAom SaltyAom added the question Further information is requested label Feb 23, 2023
@gotjoshua
Copy link
Author

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!

@SaltyAom SaltyAom added good first issue Good for newcomers and removed question Further information is requested labels Mar 14, 2023
@SaltyAom SaltyAom changed the title typia support typia/zod support Mar 14, 2023
@SaltyAom SaltyAom pinned this issue Mar 14, 2023
@murraybauer
Copy link

murraybauer commented May 6, 2023

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.

@mildronize
Copy link

@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.

@marbemac
Copy link

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

@kravetsone
Copy link

kravetsone commented Dec 13, 2023

I don't really see that big a difference DX wise from Typebox to Zod.

Unfortunately, there is a difference in DX... The TypeBox does nothing more than the JSON SCHEMA allows itself to do
Zod has excellent chainable validators/transformers, which greatly simplifies development
image

By the way, there is an interesting alternative to Zod - Valibot (Site, Github)

@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.

It's impressive! But we need to make sure that we don't lose anything from integrating this
and what if elysia has its own contribution to the type system

t.Object({
    avatar: t.File({
        type: "image",
    }),
}),

I'm not ready to give it up! It's too convenient

@em-jones
Copy link

em-jones commented Feb 9, 2024

I don't really see that big a difference DX wise from Typebox to Zod.

Two things that "jump out" at me are:

  • branded types
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.

  • ubiquity... Not to say that this alone should be a deciding factor, but a tool's popularity can be a benefit

@medv
Copy link

medv commented Feb 9, 2024

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.

@em-jones
Copy link

em-jones commented Feb 9, 2024

@medv 's sentiment:

Definitely a non-starter, would have been an instant switch from fastify otherwise.

This is an anecdotal scenario of why ubiquity can be meaningful.

(not to mention react-hook-form and its zodResolver on the other side).

To their point... what's stopping this from being an extensible interface? (besides the obnoxiousness that might come from some additional generic typing)

@fernandortec
Copy link

https://github.com/turkerdev/fastify-type-provider-zod

Can this be of any help?
This package can create swagger docs using all Zod schemas and Fastify, would an implementation like this solve the issue of generating the open API schema?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

9 participants