Utility to convert JSON Schemas and OpenAPI Declarations to Valibot schemas.
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,
});
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,
});
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,
});
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`
},
});
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
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. |
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. |