arktype icon indicating copy to clipboard operation
arktype copied to clipboard

Convert Arktype schema to JSON Schema

Open thelinuxlich opened this issue 2 years ago • 11 comments

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.

thelinuxlich avatar May 26 '23 02:05 thelinuxlich

:+1: ; 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.

stephenh avatar Nov 11 '23 17:11 stephenh

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

ssalbdivad avatar Nov 11 '23 20:11 ssalbdivad

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

floratmin avatar Dec 12 '23 11:12 floratmin

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

thetayloredman avatar Jan 21 '24 18:01 thetayloredman

@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 https://github.com/arktypeio/arktype/issues/802

ssalbdivad avatar Jan 21 '24 18:01 ssalbdivad

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

gabrielgrant avatar May 17 '24 13:05 gabrielgrant

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

ssalbdivad avatar May 17 '24 16:05 ssalbdivad

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

gabrielgrant avatar May 22 '24 17:05 gabrielgrant

@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 avatar May 22 '24 17:05 ssalbdivad

@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 avatar Jun 04 '24 20:06 ssalbdivad

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.

dearlordylord avatar Aug 07 '24 20:08 dearlordylord

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"]
}

ssalbdivad avatar Aug 19 '24 03:08 ssalbdivad