xsAI v0.2 is now available! Read Announcement
xsAI0.2.2

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
Zodv4.0+Supported
Zod Miniv4.0+Supported
Zod (with zod-to-json-schema)v3.24+Supported
Valibot (with @valibot/to-json-schema)v1.0+Supported
ArkTypev2.1+Supported
Effect Schemav3.14+Supported
Suryv10+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 'sury'
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 ()
 
const  = .({
  : .,
  : .([., .]),
}).(., { : 'My neat object schema' })
 
const  = await ()
 
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.

You can register multiple schema vendors if you need to.

import {  } from 'arktype'
import {  } from 'effect'
import * as  from 'sury'
import * as  from 'valibot'
import { ,  } from 'xsschema'
import {  } from 'zod'
 
const  = ({
  : 'string',
  : 'number | boolean',
}).('My neat object schema')
 
await ('arktype') 
const  = ()
 
const  = .( 
  .({
    : .,
    : .(., .),
  }).({ : 'My neat object schema' })
)
 
await ('effect') 
const  = ()
 
const  = .({
  : .,
  : .([., .]),
}).(., { : 'My neat object schema' })
 
await ('sury') 
const  = ()
 
const  = .(
  .({
    : .(),
    : .([.(), .()]),
  }),
  .('My neat object schema'),
)
 
await ('valibot') 
const  = ()
 
const  = .({
  : .(),
  : .([.(), .()]),
}).('My neat object schema')
 
await ('zod') 
const  = ()

validate

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

Last updated on

On this page