valibot icon indicating copy to clipboard operation
valibot copied to clipboard

[Feature Request] Generate Valibot Schema from JSON Schema

Open mmospanenko opened this issue 8 months ago • 8 comments

I didn't find similar topics, toJSON is more popular I see. But I have a very common use case: when we generated TS API services and models (including json schemas) from OpenAPI spec (using Hey Api for ex), it will be great to have Valibot schemas too.

Is it possible theoretically? Will be awesome to have CLI

mmospanenko avatar Mar 13 '25 05:03 mmospanenko

Yes, this is possible, but I don't have the time to implement it right now. Are you interested in investigating the implementation of such a package?

fabian-hiller avatar Mar 14 '25 23:03 fabian-hiller

I’m interested too, and would be willing to give a try.

muningis avatar Mar 20 '25 10:03 muningis

Amazing! Do you want to start it as a community project or as an official package of this repo?

fabian-hiller avatar Mar 21 '25 03:03 fabian-hiller

Was thinking maybe separate repo until it’s somewhat ready, but I would be more than fine to have it on official repo

muningis avatar Mar 21 '25 05:03 muningis

You might find inspiration in json-schema-to-zod and other transform.tools.

mxdvl avatar Mar 21 '25 09:03 mxdvl

Thanks, will take a look!

However, my current plan is to output those schemas as a code, so we can generate schemas code, so we don't have to fetch JSON Schema or OpenAPI Declarations to the client

muningis avatar Mar 21 '25 09:03 muningis

Initial implementation is ready here: https://github.com/muningis/to-valibot. Need to do some more fine tuning, and then we can move it over.

Currently it supports OpenAPI yaml/json declarations and JSON Schema. Unsupported features of schemas:

  • anyOf/allOf/oneOf/not
  • discriminated union type arrays
  • various formats for string type

muningis avatar Mar 22 '25 09:03 muningis

Thank you so much! I added a comment to PR #1099

fabian-hiller avatar Mar 23 '25 16:03 fabian-hiller

Hi, @mmospanenko. I'm Dosu, and I'm helping the Valibot team manage their backlog. I'm marking this issue as stale.

Issue Summary:

  • You suggested adding a feature to generate Valibot schemas from JSON schemas.
  • fabian-hiller acknowledged the idea but mentioned a lack of time to implement it.
  • muningis expressed interest in developing the feature and started a project at https://github.com/muningis/to-valibot.
  • The project supports OpenAPI yaml/json declarations and JSON Schema, with some limitations.
  • fabian-hiller appreciated the effort and mentioned it in PR #1099.

Next Steps:

  • Please let me know if this issue is still relevant to the latest version of the Valibot repository by commenting here.
  • If there is no further activity, this issue will be automatically closed in 30 days.

Thank you for your understanding and contribution!

dosubot[bot] avatar Jun 22 '25 16:06 dosubot[bot]

Sorry guys for long response, I appreciate your involving and quick implementation, and I believe that everything important were covered, I will plan migration and will add some feedback. Thank you so much!

mmospanenko avatar Jul 23 '25 06:07 mmospanenko