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

Convert Arktype schema to JSON Schema #776

Closed
thelinuxlich opened this issue May 26, 2023 · 14 comments
Closed

Convert Arktype schema to JSON Schema #776

thelinuxlich opened this issue May 26, 2023 · 14 comments

Comments

@thelinuxlich
Copy link

One thing I'm gonna miss once I migrate to Arktype with Zodios is the capability of converting the contract schema to JSON Schema for Swagger and automatically generate an OpenAPI endpoint.

@stephenh
Copy link

👍 ; I need a json schema for a configuration file that I want to have checked/auto-completed in vscode/IDEs, and don't want to write the json schema by hand.

I'd love to use Arktype b/c your schemas are so amazing/succinct, but am currently using zod solely b/c of the zod-to-json-schema capability.

@ssalbdivad
Copy link
Member

Thanks for the feedback. This will definitely be one of the first things we prioritize in beta given how much interest there is.

@floratmin
Copy link

floratmin commented Dec 12, 2023

This would also allow to use something like fast-json-stringify for fast serialization.

@thetayloredman
Copy link
Contributor

thetayloredman commented Jan 21, 2024

@ssalbdivad I would be interested in taking a stab at this when you believe it's ready to approach—along with its inverse (JSON Schema to AT)

@ssalbdivad
Copy link
Member

@thetayloredman After I finish the props compilation stuff I'm working on this would be doable, which ideally would approximately line up with your work on #802

@gabrielgrant
Copy link
Contributor

Curious for an update on this -- iiuc the plan had originally been to do this post 1.0 beta, right? But imagine it got pushed back to accommodate the changes in 2.0 ?

Where do we stand, with that now ready to go? Any thoughts about how to best move forward with this?

FWIW, I love the work you've been doing on AT overall, it looks amazing and I'm super excited to start using it, but some path to getting JSON Schema out is a hard requirement for me (whether that's a native export functionality or via zod or TypeBox (as discussed in #769 ))

@ssalbdivad
Copy link
Member

@gabrielgrant My current priorities are:

  1. Docs for 2.0
  2. Finish remaining 2.0 announced features, generics and matching (mostly done)
  3. Announce 2.0 and spend a bit of time on marketing

Then I'd prioritize the most requested issues which are this and OpenAPI integration. I'd guess maybe in a month or so I might start on this?

If you're interested, this may be fairly tractable for an external contributor now that the internal type system has stabilized and has a clear structure for compilation.

I think the hardest part would be determining which types aren't supported by JSON schema and handling those cases appropriately.

@gabrielgrant
Copy link
Contributor

Moving from discord...

Would love to dive a bit more into what would be needed to add JSON schema support to see if that might be something I could undertake

It seems like the most straightforward way to do this is to just walk a type's AST (myType.flat) and transpile each node to its JSON Schema equivalent (if one exists). For type constraints that aren't perfectly representable, I'm thinking the two options are to either:

  1. output the closest approximation
  2. to just fail loudly.

Probably the 2. should be the default (as it is safer), with the 1. as an opt-in option? (but thinking it would be reasonable to only do 2. for an initial implementation?)

@ssalbdivad
Copy link
Member

@gabrielgrant Yes I agree it should just fail for now on anything JSON schema doesn't represent.

That said, it should definitely be built on top of 2.0, and there have been huge changes to the type system since then (which I think should make stuff like this much easier).

Probably the easiest way to go over context would be to hop on a call for 15-30 minutes so I can go over the structure of the type system and we can talk about approaches for integrating it.

@ssalbdivad
Copy link
Member

@gabrielgrant Just wanted to check in if you're still working on this?

No pressure, if not I may have someone else interested in tackling it.

@ssalbdivad ssalbdivad moved this from Backlog to In progress in arktypeio Jun 19, 2024
@ssalbdivad ssalbdivad moved this from In progress to Planned in arktypeio Jul 9, 2024
@ssalbdivad ssalbdivad moved this from Planned to Backlog in arktypeio Jul 26, 2024
@dearlordylord
Copy link
Contributor

It may be beneficial for popularity now: openai uses json schema for defining structured output, starting today. They used Zod and zod-to-json-schema by default for their JS api.

@ssalbdivad
Copy link
Member

This feature was added in 2.0.0-beta.6

From the release notes:

Convert a Type instance to an equivalent JSON Schema. Will throw a ParseError if the Type contains constraints not supported by JSON Schema or morphs.

import { type } from "arktype"

const user = type({
	name: "string",
	"age?": "number"
})

const jsonSchema = user.toJsonSchema()
{
	"type": "object",
	"properties": {
		"name": {
			"type": "string"
		},
		"age": {
			"type": "number"
		}
	},
	"required": ["name"]
}

@ciscoheat
Copy link

As I'm using the JSON Schema to generate default values, it would be great if you could supply some options to the toJsonSchema method, for example a callback that can manage unsupported types.

@ssalbdivad
Copy link
Member

@ciscoheat Yeah this would definitely be useful. If you're able to create a separate issue, that would be a big help for tracking!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done (merged or closed)
Development

No branches or pull requests

8 participants