Skip to content

muningis/to-valibot

Repository files navigation

(OpenAPI Declaration, JSON Schema) to Valibot

Utility to convert JSON Schemas and OpenAPI Declarations to Valibot schemas.

Example usage

1. Converting list of OpenAPI Declarations in .json format to Valibot

import { valibotGenerator } from "to-valibot";

const { generate } = valibotGenerator({
  outDir: "./src/types",
});

const schemas = await Promise.all([
  fetch("http://api.example.org/v2/api-docs?group=my-awesome-api").then(r => r.json()),
  fetch("http://api.example.org/v2/api-docs?group=other-api").then(r => r.json()),
  fetch("http://api.example.org/v2/api-docs?group=legacy-api").then(r => r.json()),
]);

await generate({
  format: 'openapi-json',
  schemas,
});

2. Converting OpenAPI Declarations in .yaml format to Valibot

import { readFile } from "node:fs/promises";;
import { valibotGenerator } from "to-valibot";

const { generate } = valibotGenerator({
  outDir: "./src/types",
});

const schema = readFile("./declarations/my-awesome-api.yaml");

await generate({
  format: 'openapi-yaml',
  schema,
});

3. Converting JSON Schema to Valibot

import { readFile } from "node:fs/promises";;
import { valibotGenerator } from to-valibot";
import schema from "~/schemas/my-api.json";

const { generate } = valibotGenerator({
  outDir: "./src/types",
});

await generate({
  format: 'json',
  schema,
});

3. Provide name to your schemas

import { readFile } from "node:fs/promises";;
import { valibotGenerator } from to-valibot";
import schema from "~/schemas/my-api.json";

const { generate } = valibotGenerator({
  outDir: "./src/types",
});

await generate({
  format: 'json',
  schemas: {
    "My Awesome API Schema": schema // will output to `./src/types/my-awesome-api-schema.ts`
  },
});

API

valibotGenerator

valibotGenerator accepts options object with these parameters

Name Type Required Description
outDir string yes Declare in which directory generated schema(s) should be written

generate

generate function returned by valibotGenerator accepts different set of options, depending on format.

Name Type Required Description
format 'openapi-yaml' yes Format specification for the generated output
schema string no* Single schema to be processed
schemas string[] no* Multiple schemas to be processed
* Either schema OR schemas must be provided, but not both.
Name Type Required Description
format 'openapi-json' | 'json' yes Format specification for the generated output
schema string | object no* Single schema to be processed
schemas (string | object)[] no* Multiple schemas to be processed
* Either schema OR schemas must be provided, but not both.

Supported features

Same set of features are supported both in OpenAPI Declarations and JSON Schemas

Feature Status Note
required
description
const ⚠️ Only works with primitive values
- - -
string
enum
minLength
maxLength
pattern
format="email"
format="uuid"
format="date-time"
format="date"
format="time"
format="duration" ⚠️ fabian-hiller/valibot#1102
format="idn-email"
format="hostname"
format="idn-hostname"
format="ipv4"
format="ipv6"
format="json-pointer"
format="relative-json-pointer"
format="uri"
format="uri-reference"
format="uri-template"
format="iri"
format="iri-reference"
- - -
number
integer
exclusiveMaximum
exclusiveMinium
maximum
minium
multipleOf
- - -
array ⚠️ Only single array item kind is supported for now
minItems
maxItems
uniqueItems
prefixItems
contains
minContains
maxContains
- - -
object
patternProperties
additionalProperties
minProperties
maxProperties
- - -
boolean
null
- - -
anyOf
allOf
oneOf
not ⚠️ Unfortunately type inference for this is not working.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published