-
-
Notifications
You must be signed in to change notification settings - Fork 65
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
Comments
👍 ; 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. |
Thanks for the feedback. This will definitely be one of the first things we prioritize in beta given how much interest there is. |
This would also allow to use something like |
@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 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 |
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 My current priorities are:
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. |
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 (
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 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. |
@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. |
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. |
This feature was added in 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"]
} |
As I'm using the JSON Schema to generate default values, it would be great if you could supply some options to the |
@ciscoheat Yeah this would definitely be useful. If you're able to create a separate issue, that would be a big help for tracking! |
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.
The text was updated successfully, but these errors were encountered: