@skmtc/gen-valibot
OpenAPI to Valibot schema generator for Skmtc.
Generate type-safe Valibot validation schemas from your OpenAPI specifications. Valibot is a modular, lightweight schema library with excellent TypeScript support and tree-shaking capabilities.
Installation
Install Deno
# On MacOS/Linux
curl -fsSL https://deno.land/install.sh | sh
# On Windows
irm https://deno.land/install.ps1 | iex
Install Skmtc
deno install -g -A --unstable-worker-options jsr:@skmtc/cli@0.0.388 -n skmtc -f
Create project and generate artifacts using TUI (recommended)
# Create project then Generate artifacts
skmtc
Create project and generate artifacts using CLI
# Create project
skmtc init <project name>
# Install Valibot generator
skmtc install @skmtc/gen-valibot <project name>
# Bundle generator code
skmtc bundle <project name>
# Generate artifacts from OpenAPI schema
skmtc generate <project name> <path or url to openapi schema>
Usage Examples
Basic Types
| Input (OpenAPI Schema) | Valibot Schema |
|---|
{
"components": {
"schemas": {
"User": {
"type": "object",
"properties": {
"id": { "type": "string" },
"age": { "type": "number" },
"isActive": { "type": "boolean" }
},
"required": ["id", "age"]
}
}
}
}
|
import * as v from "valibot"
export const User = v.object({
id: v.string(),
age: v.number(),
isActive: v.optional(v.boolean())
})
|
Enums and Literals
| Input (OpenAPI Schema) | Valibot Schema |
|---|
{
"Status": {
"type": "string",
"enum": ["active", "inactive", "pending"]
},
"Role": {
"type": "string",
"enum": ["admin"]
}
}
|
export const Status = v.picklist(["active", "inactive", "pending"])
export const Role = v.literal("admin")
|
Arrays and Nested Objects
| Input (OpenAPI Schema) | Valibot Schema |
|---|
{
"Team": {
"type": "object",
"properties": {
"name": { "type": "string" },
"members": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": { "type": "string" },
"role": { "type": "string" }
},
"required": ["id", "role"]
}
}
},
"required": ["name", "members"]
}
}
|
export const Team = v.object({
name: v.string(),
members: v.array(v.object({
id: v.string(),
role: v.string()
}))
})
|
Nullable and Optional Fields
| Input (OpenAPI Schema) | Valibot Schema |
|---|
{
"Profile": {
"type": "object",
"properties": {
"bio": {
"type": "string",
"nullable": true
},
"website": { "type": "string" }
},
"required": ["bio"]
}
}
|
export const Profile = v.object({
bio: v.nullable(v.string()),
website: v.optional(v.string())
})
|
Union Types
| Input (OpenAPI Schema) | Valibot Schema |
|---|
{
"Pet": {
"oneOf": [
{
"type": "object",
"properties": {
"type": { "type": "string", "enum": ["cat"] },
"meow": { "type": "boolean" }
},
"required": ["type", "meow"]
},
{
"type": "object",
"properties": {
"type": { "type": "string", "enum": ["dog"] },
"bark": { "type": "boolean" }
},
"required": ["type", "bark"]
}
]
}
}
|
export const Pet = v.union([
v.object({
type: v.literal("cat"),
meow: v.boolean()
}),
v.object({
type: v.literal("dog"),
bark: v.boolean()
})
])
|
String Formats
| Input (OpenAPI Schema) | Valibot Schema |
|---|
{
"Event": {
"type": "object",
"properties": {
"createdAt": {
"type": "string",
"format": "date-time"
}
},
"required": ["createdAt"]
}
}
|
export const Event = v.object({
createdAt: v.pipe(v.string(), v.isoDateTime())
})
|
Additional Properties
| Input (OpenAPI Schema) | Valibot Schema |
|---|
{
"Metadata": {
"type": "object",
"additionalProperties": { "type": "string" }
}
}
|
export const Metadata = v.record(v.string(), v.string())
|
With both properties and additionalProperties:
| Input (OpenAPI Schema) | Valibot Schema |
|---|
{
"Config": {
"type": "object",
"properties": {
"id": { "type": "string" }
},
"required": ["id"],
"additionalProperties": { "type": "number" }
}
}
|
export const Config = v.intersect([
v.object({ id: v.string() }),
v.record(v.string(), v.number())
])
|
Supported Features
Testing
Run tests for this generator:
cd gen-valibot && deno task test
Or with coverage:
deno task test:coverage
Support
License
MIT.