xsAI v0.1 is now available! Read Announcement
xsAI0.2.0-beta.1

xsschema

extra-small, Standard Schema-based alternative to typeschema.

install sizeminified sizeminzipped size
npm i xsschema

Coverage

validate doesn't require manual support and works with any Standard Schema-compatible library.

Here is the list of libraries that toJsonSchema is compatible with:

ImplementerVersion(s)Status
Zod3.24.0+Supported
Valibotv1.0+Supported
ArkTypev2.0+Supported
Effect Schemav3.13.0+Supported

Usage

If you're using xsAI, this package is a dependency of some of those packages - you don't need to install it separately.

toJsonSchema

import {  } from 'arktype'
import {  } from 'effect'
import * as  from 'valibot'
import {  } from 'xsschema'
import {  } from 'zod'
 
const  = ({
  : 'string',
  : 'number | boolean',
}).('My neat object schema')
 
const  = await ()
 
const  = .({
  : .,
  : .(., .),
}).({ : 'My neat object schema' })
 
const  = await (
  // https://github.com/Effect-TS/effect/issues/4494
  .(, .()) 
)
 
const  = .(
  .({
    : .(),
    : .([.(), .()]),
  }),
  .('My neat object schema'),
)
 
const  = await ()
 
const  = .({
  : .(),
  : .([.(), .()]),
}).('My neat object schema')
 
const  = await ()

toJsonSchemaSync (experimental)

toJsonSchema is async (because it has to await import the vendor's dependencies), but we also support a synchronous version: toJsonSchemaSync.

Before using toJsonSchemaSync, you must call initToJsonSchemaSyncVendor with your schema vendor.

import {  } from 'xsschema'
 
await ('arktype')
// or
await ('effect')
// or
await ('valibot')
// or
await ('zod')

You can register multiple schema vendors if you need to.

import {  } from 'arktype'
import {  } from 'effect'
import * as  from 'valibot'
import {  } from 'xsschema'
import {  } from 'zod'
 
const  = ({
  : 'string',
  : 'number | boolean',
}).('My neat object schema')
 
const  = ()
 
const  = .({
  : .,
  : .(., .),
}).({ : 'My neat object schema' })
 
const  = (
  // https://github.com/Effect-TS/effect/issues/4494
  .(, .()) 
)
 
const  = .(
  .({
    : .(),
    : .([.(), .()]),
  }),
  .('My neat object schema'),
)
 
const  = ()
 
const  = .({
  : .(),
  : .([.(), .()]),
}).('My neat object schema')
 
const  = ()

validate

import {  } from 'xsschema'
import {  } from 'arktype'
import * as  from 'valibot'
import * as  from 'zod'
 
const  = ("string")
const  = .()
const  = .()
 
const  = await (, '123')
const  = await (, '123')
const  = await (, '123')
Edit on GitHub

Last updated on

On this page