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

Support arktype to zod function #769

Open
scarf005 opened this issue May 22, 2023 · 9 comments
Open

Support arktype to zod function #769

scarf005 opened this issue May 22, 2023 · 9 comments

Comments

@scarf005
Copy link

Request a feature

being able to output zod schema will help arktype bootstrap into existing zod ecosystem.

🤷 Motivation

while i love how concise arktype is, i am unable to use it as there is currently no way to generate JSON schema.

💡 Solution

provide arktypeToZod function which will convert a arktype type into zod schema. This will help arktype's adoptation as now types made from arktype can utilize existing zod to X ecosystem.

@ssalbdivad
Copy link
Member

@sinclairzx81 was actually working on something like this recently! I could definitely see the value in having something like this available in the ecosystem, although it would never be a part of the core package itself.

That said, we do eventually plan to allow direct compilation to/from JSON schema so hopefully that will be a big help in terms of standardization.

@sinclairzx81 let me know if the work you're doing in TypeBox would support go-between mappings to Zod and JSON schema, otherwise we'll try and whip something up to cover the straightforward conversions after the beta release.

@ssalbdivad ssalbdivad moved this from To do to Backlog in arktypeio May 22, 2023
@sinclairzx81
Copy link

@ssalbdivad Heya,

Currently the Workbench project only supports TypeScript to TypeBox transformation (via compiler transform). Once the TypeScript types are encoded as TypeBox types (with the encoding referred to as the TypeBoxModel), I apply a secondary transform from the Model out to other formats (JSONSchema (naturally), JavaScript (validation, value generation), Zod, etc). In this regard, the TypeBoxModel is a pivot point for transformation.

Type X -> TypeBoxModel -> Type Y

To support something like Zod to JSON Schema, I'd need to write something to evaluate the Zod types into the TypeBox Model (or use the compiler to map Zod into the TypeBoxModel). This is somewhat out of scope for the Workbench (or Codegen backend, i guess???), but I agree there would be value in having such transformations.

I'm open to hearing ideas on providing a more generalized transformation tool for community runtime type systems. And would be more than happy to contribute a TB to such a effort (I'm getting asked about code generation more and more these days, so it's worth exploring)

@ssalbdivad
Copy link
Member

@sinclairzx81 Ahh okay got it, it looked from some of the code you sent for the workbench that it was potentially reusable for these kind of transformations, but maybe there is a part of it I'm missing that would require its own effort.

Happy to coordinate on this as soon as we wrap up the beta release!

@gabrielgrant
Copy link
Contributor

iiuc @sinclairzx81 correctly, it sounds like plugging into the TypeBox ecosystem entails exporting a TypeBoxModel, which describes the types and constraints using TSchema

This would open up JSON Schema creation, as well as transpilation to Zod, JSON Schema, raw JS/TS, etc

@ssalbdivad any thought on what would be entailed in making this happen?

@ssalbdivad
Copy link
Member

ssalbdivad commented Dec 11, 2023

Compiling to JSON schema is a top priority! It seems like from there we will have lots of options to leverage that as an intermediate layer to other validators.

It is currently our most upvoted open issue: #776

@gabrielgrant
Copy link
Contributor

Yes, to be clear, I'm wondering if an easier (and more flexible) path forward might be to generate a TypeBoxModel representation, and use that to generate JSONSchema (and other transpilation targets), rather than outputting JSONSchema directly. AFAIU There isn't currently a way to go from JSONSchema to a TypeBoxModel (unless that's been added recently @sinclairzx81 ?)

@ssalbdivad
Copy link
Member

@gabrielgrant The first priority would be to generate outputs that conform to an industry standard like JSON-schema, as @sinclairzx81 would advocate for. Those integrations can be built into the core package itself.

TypeBox may be useful as an intermediate layer to Zod, but that would be a utility package rather than part of the core functionality.

@gabrielgrant
Copy link
Contributor

gabrielgrant commented Dec 11, 2023

Yes, that makes sense that you wouldn't want to pull TypeBox in to the core for a long-term solution

Mostly was wondering if you have any pointers for where to start poking around the arktype internals to see if this translation (to either TypeBox or JSONSchema directly) is something I might be able to undertake (or just if you'd put any thought to how you'd implement it yet)

@ssalbdivad
Copy link
Member

Ahh. It is probably best to hold off on this until I wrap up the beta work I am doing as that node structure will be critical to the compilation process and has changed drastically since alpha.

I should at least have a prerelease finished in the next few days that would be stable enough to start working on a TypeBox translation layer if you're interested in working on that then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Backlog
Development

No branches or pull requests

4 participants