valibot-json-schema icon indicating copy to clipboard operation
valibot-json-schema copied to clipboard

Provide JSON schema translation as a core package

Open fabian-hiller opened this issue 1 year ago • 6 comments
trafficstars

Hi Guillaume, I am toying with the idea of providing an official JSON schema translation package. It seems to me that this is more important than I initially thought due to the use of schema libraries with AI tools.

If the source code of the JSON schema translation lives next to the source code of the core library, it should be easier to keep it up to date. What do you think? Would you be interested in joining this effort?

fabian-hiller avatar Aug 11 '24 09:08 fabian-hiller

Sure it sounds great! Would it be only conversion from valibot to JSON schema or also the inverse? Do you have any idea how you would want to do that in valibot? Having it modularized like all schemas/actions or just centralized conversion function like this lib? Also this lib has a CLI, I don't know if you'd want that in valibot (I could keep it in this repo)

gcornut avatar Aug 12 '24 18:08 gcornut

For now, my idea is just to add a @valibot/to-json-schema package with a recursive and centralized function to convert Valibot schemas to JSON schemas. A CLI or inverse conversion is currently out of my scope.

fabian-hiller avatar Aug 13 '24 00:08 fabian-hiller

Ok well if you want I can draft a PR to create a new to-json-schema package in the valibot repository

gcornut avatar Aug 13 '24 17:08 gcornut

This is amazing, i was just looking through openAI's docs for structured output and found it only supported zod. https://github.com/openai/openai-node/blob/master/src/helpers/zod.ts

I'm trying to integrate it with valibot so I'd be happy to be the guinea pig for testing this in prod whenever y'all get the chance :D

CestDiego avatar Aug 13 '24 21:08 CestDiego

@gcornut that would create! Feel free to add the package to /packages/to-json-schema. Please try to reuse the basic package setup from /library. Also, I recommend adding only the implementation and tests for object, string, and email so that we can discuss the optimal implementation before implementing everything. One idea I had in mind is to add a force: boolean parameter so that users can decide whether to throw or ignore if a part of a schema can't be converted to JSON schema.

fabian-hiller avatar Aug 15 '24 11:08 fabian-hiller

@gcornut do you have an update for me? If you don't have time to implement it at the moment, I can do it or reach out to the community and ask for help.

fabian-hiller avatar Aug 22 '24 11:08 fabian-hiller

I'm off this week but I already have some code ready. I should be able to open the PR in 2/3 days 👍

gcornut avatar Aug 23 '24 08:08 gcornut

Ok, don't stress about it and enjoy your free time! Next week will be enough.

fabian-hiller avatar Aug 23 '24 11:08 fabian-hiller