-
-
Notifications
You must be signed in to change notification settings - Fork 204
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
Initialize @valibot/to-json-schema package #802
Initialize @valibot/to-json-schema package #802
Conversation
Thank you! I will probably give you feedback on everything by tomorrow. |
Thanks again for the PR. I have reviewed everything and also investigated several alternative implementations. I even tried a fully typesafe implementation where the output type was the actual JSON schema output. Unfortunately, the typesafe implementation got quite complex and probably too hard to maintain at this point. Since Valibot is not a JSON-schema-first library, I don't expect many users to expect such an implementation. However, I have added an alternative implementation to |
@fabian-hiller You alternative implementation looks good 👍 |
Thank you! Would you change anything? Otherwise, feel free to clean up the code and add support for more schemas and actions after we answer my next question. I saw that TypeBox's |
We should also check the integration with OpenAI and Vercel AI (related Issue) before extending our implementation. |
I don't see how JSON schema could support
Yes their are newer versions of the JSON schema specs (which some major changes 😬) but most tools use the draft-07 version (OpenAI, Vercel AI, etc.) And actually if we explicitly want to support OpenAI structured output we would need to have a subset of JSON schema since it does not support every feature. For Vercel AI I can't see document that suggest they don't support all JSON schema features 🤷 (they use the same TypeScript JSONSchema7 type as we do). We could add an option |
Some people JSON schema only for documentation purpose so maybe for them we could add another output "target" that could output things like |
Thanks for your detailed answer. For now, I think it is best to optimize everything for JSON Schema Draft 7 and thread everything else as not supported ( |
How strict do you think this lib should be on the input schema ? I was thinking that the default value in optional/null/nullish schemas and the value in the literal schema should only be JSON compatible values. And actually these values needs to be strictly compatible, excluding types like Maybe the actions we convert should check on which type they are applied ( It's probably overkill but we could validate the input schema via a valibot meta schema 😄 |
I think that
I think we should not worry about that. Our API is meant to be used in a certain way, and we should not have to deal with people using it incorrectly. |
41ae798
to
4a7ee57
Compare
cd11852
to
d556711
Compare
commit: |
Alright so I've rebased from the main branch. I've added almost all possible schema & action conversions. I need to add optional conversions for date and bigint since they are not technically supported in JSON but can be converted to number or string depending on the use case. |
Thank you so much! It is ok for me if this PR is "big". I will try to review everything in the next days with the goal to release this package next week. |
d556711
to
efdafe1
Compare
Ok I've added optional date and bigint schema conversion (requiring "strategy" configs) |
I am not sure if we should add the strategy configs from the beginning. I would prefer to wait until people come up with good use cases and then decide on the implementation. But feel free to share your opinion since your conversion library has a longer history. |
I started refactoring some parts and will commit my changes later. We will probably have a few implementation interactions in the next days before releasing the first version of the package next week. |
I refactored the code and made the following bigger changes: Definitions: I removed the Assert JSON: I replaced or removed the Sorry for partially overwriting or changing your code without asking, but I think this will help us ship the first version of this package faster. Please let me know if you want to revert or change any of my modifications. I did not touch the tests. We can do that after we agree on the final implementation. |
Named definitions seems an essential feature to me. I know I would have to hack it back into my scripts that exports my valibot schemas into JSON schema. But I won't be blocking just for that if you want to proceed 👍 |
I agree that this could be an essential feature. Therefore, I added support for explicitly specified definitions with my last commit. |
Should I continue with the final review and unit tests, or do you have more feedback on the current implementation? |
All good for me 👍 |
I think that I will merge this PR later (I am not done yet) and ship our v0.1.0 release 🎉 |
@gcornut do you think we should completely ignore unsupported schemas and actions, or is it better to try to get it right? A concrete example is |
If we choose the second strategy, we might consider removing the |
Both solutions sound ok to me as long as the conversion is strict by default. Like mentioned earlier there is definitely some people who just want to output schemas for documentation purpose and don't care about having 100% correct JSON schema definitions. But maybe we could look into a "loose" output format later |
What do you think about removing |
Sounds ok. If some user need to be noticed that the conversion was not completely successful we can think of adding another option later 👍 |
I think I will keep |
Thanks so much @gcornut for your contribution! We are ready to merge! 🍾 |
v0.1.0 is available |
Following the discussions on gcornut/valibot-json-schema#84, here is my proposal on a new package for converting Valibot to JSON schema to be moved here next to Valibot core library code :)
The setup
@valibot/to-json-schema
any
schemanull
schemastring
schemaobject
schemaemail
validationI tried to match the code style of the core Valibot library.
What is next ?
@gcornut/valibot-json-schema
Date
intoformat: 'date-time'
orformat: 'unix-time'
,BigInt
intoformat: 'int64'
or string, explicitundefined
intonull
, etc.)=> Some tools (like OpenAI structured output) does not support all the features of JSON schema. Maybe we could add a configuration to skip those features ?
I'll deprecate and archive
@gcornut/valibot-json-schema
once this lib gets to feature parity. Although I will keep the CLI in a new repo because it's a use case I still need.