Giter Site home page Giter Site logo

Valibot to JSON Schema about valibot HOT 9 CLOSED

fabian-hiller avatar fabian-hiller commented on May 21, 2024 5
Valibot to JSON Schema

from valibot.

Comments (9)

shakyShane avatar shakyShane commented on May 21, 2024 3
  • 1

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

from valibot.

gcornut avatar gcornut commented on May 21, 2024 2

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 #263

from valibot.

fabian-hiller avatar fabian-hiller commented on May 21, 2024 1

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.

from valibot.

noxify avatar noxify commented on May 21, 2024 1

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

from valibot.

gcornut avatar gcornut commented on May 21, 2024 1

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 🤔

from valibot.

fabian-hiller avatar fabian-hiller commented on May 21, 2024 1

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.

from valibot.

fabian-hiller avatar fabian-hiller commented on May 21, 2024 1

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.

from valibot.

gcornut avatar gcornut commented on May 21, 2024

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<typeof FooElementSchema>;
  1. then execute npx ts-json-schema-generator --path src/schema.ts -t FooElement -o dist/schema.json

The result:

{
  "$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"
    }
  }
}

from valibot.

fabian-hiller avatar fabian-hiller commented on May 21, 2024

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.

from valibot.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.