Skip to content

Convert Arktype schema to JSON Schema #776

Closed

Description

@thelinuxlich

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.

Activity

  1. moved this from To do to Backlog in arktypeioon May 26, 2023
  2. stephenh commented on Nov 11, 2023

    @stephenh

    👍 ; 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.

  3. ssalbdivad commented on Nov 11, 2023

    @ssalbdivad
    Member

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

  4. floratmin commented on Dec 12, 2023

    @floratmin

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

  5. thetayloredman commented on Jan 21, 2024

    @thetayloredman
    Contributor

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

  6. ssalbdivad commented on Jan 21, 2024

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

  7. gabrielgrant commented on May 17, 2024

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

  8. ssalbdivad commented on May 17, 2024

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

  9. gabrielgrant commented on May 22, 2024

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

  10. ssalbdivad commented on May 22, 2024

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

  11. ssalbdivad commented on Jun 4, 2024

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

  12. moved this from Backlog to In progress in arktypeioon Jun 19, 2024
  13. moved this from In progress to Planned in arktypeioon Jul 9, 2024
  14. moved this from Planned to Backlog in arktypeioon Jul 26, 2024
  15. dearlordylord commented on Aug 7, 2024

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

  16. ssalbdivad commented on Aug 19, 2024

    @ssalbdivad
    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"]
    }
  17. moved this from Implemented to Done (merged or closed) in arktypeioon Aug 19, 2024
  18. ciscoheat commented on Dec 15, 2024

    @ciscoheat

    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.

  19. ssalbdivad commented on Dec 16, 2024

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    • Status
      Done (merged or closed)

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions