fabian-hiller / valibot

The modular and type safe schema library for validating structural data 🤖
https://valibot.dev
MIT License
6.26k stars 202 forks source link

Valibot to JSON Schema #23

Closed huypham50 closed 11 months ago

huypham50 commented 1 year ago

Will there a native valibot ↔ json schema conversion?

I think this is what missing from zod (you have to use zod-to-json-schema, which is a third party package with some known issues)

Whereas in the python ecosystem, pydantic has native support to generate json schemas from models.

Lovely work so far!

shakyShane commented 1 year ago

but, I would also enjoy the other way around - json-schema to valibot schemas - perhaps we can leverage existing tools around zod/arktype for this...

fabian-hiller commented 1 year ago

It's on my list to check out a possible implementation. Since I'm currently writing my bachelor's thesis, I won't get to it until September.

noxify commented 1 year ago

Maybe https://github.com/sinclairzx81/typebox could be used as inspiration?

gcornut commented 1 year ago

There is one existing solution to convert from valibot to json schema. You can generate json schema from the TS types with ts-json-schema-generator. Of course it's not a perfect solution because some of the features of valibot do not translate to TS types but would translate to JSON schema (like minLength to constrain array size).

An example on how to use this:

  1. define a schema

    src/schema.ts

    
    import { object, literal, type Input } from 'valibot';

const FooElementSchema = object({ type: literal('foo') }); export type FooElement = Input;

2. then execute `npx ts-json-schema-generator --path src/schema.ts -t FooElement -o dist/schema.json`

**The result:**

```json
{
  "$ref": "#/definitions/FooElement",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "definitions": {
    "FooElement": {
      "additionalProperties": false,
      "description": "Input inference type.",
      "properties": {
        "type": {
          "const": "foo",
          "description": "Input inference type.",
          "type": "string"
        }
      },
      "required": [
        "type"
      ],
      "type": "object"
    }
  }
}
fabian-hiller commented 1 year ago

Thanks for the tip. Here is what I can currently say about this issue: We are working in PR #211 to make the properties of the schemas and validation functions accessible. This would theoretically allow direct serialization. However, it is important to note that serialization is not a goal of Valibot. This has to do with the fact that Valibot prioritizes functionality, bundle size and DX over serialization, since not every functionality can be serialized. Also, validating against a JSON schema as in TypeBox prevents tree shaking and code splitting. In the long run, however, I can imagine offering a JSON variant with the same API under valibot/json.

gcornut commented 1 year ago

I've started to build myself a small CLI tool to convert from Valibot to JSON schema: https://github.com/gcornut/valibot-json-schema

It loads a JS/TS module with esbuild-runner and manually converts the exported Valibot schemas into JSON schema. For my use case I don't mind having a big JS module doing the conversion of all schemas but I totally see how that would be a problem in a modular library like Valibot.

Like you say, some more metadata on schema validations (minLength, email, etc.) would help. In the meantime I might manually attach some custom metadata. Something like:

import { withJSONSchemaFeatures } from '@gcornut/valibot-json-schema/extension';
const MyArray = withJSONSchemaFeatures(array([minLength(1)]), { minItems: 1 });
// => returns the ArraySchema with a custom Symbol keyed property used by internally during conversion to JSON schema

It's redundant but it's could be a cheap & easy way to do this 🤔

fabian-hiller commented 1 year ago

Thank you, thats great! I plan to merge PR #211 this week or next. If your library works reliably, feel free to add it to our ecosystem page.

gcornut commented 12 months ago

Thank you, thats great! I plan to merge PR #211 this week or next. If your library works reliably, feel free to add it to our ecosystem page.

Ok my tool/library to convert to JSON schema is now pretty complete thanks to the new reflections API on pipe validation!
I opened a PR to add it to the ecosystem page https://github.com/fabian-hiller/valibot/pull/263

fabian-hiller commented 11 months ago

A native Valibot to JSON schema or JSON schema to Valibot implementation is not currently planned. Technically, however, it is largely possible through our new reflection API and can be covered by third-party packages like valibot-json-schema. If JSON Schema is important to you, I suggest you take a look at TypeBox.

I am therefore closing this issue. However, you can still contact me through this issue if you have any questions or feedback.

fabian-hiller commented 2 months ago

https://valibot.dev/guides/json-schema/