xsAI v0.2 is now available! Read Announcement
xsAI0.3.0-beta.8

xsschema

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

install sizeminified sizeminzipped size
npm i xsschema

Why xsSchema?

  • Actively maintained along with xsAI.
  • Support for up to libraries in the Standard Schema scope.
  • Already used by xsAI, xsMCP, FastMCP, Airi and others.

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
Zod (zod/v4)v3.25+Supported
Zod Mini (zod/v4-mini)v3.25+Supported
Zod (zod/v3, with zod-to-json-schema)v3.25+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')

jsonSchema

Define a JSON Schema.

import {  } from 'xsschema'
 
const  = ({
  : 'object',
  : {
    : {
      : 'The unique identifier for a product',
      : 'integer'
    }
  }
})

strictJsonSchema

Define a JSON Schema and set additionalProperties to false.

import {  } from 'xsschema'
 
const  = ({
  : 'object',
  : {
    : {
      : 'The unique identifier for a product',
      : 'integer'
    }
  }
})
 
// false
.(.)

Types

Schema

Pre-bundled version of StandardSchemaV1 in @standard-schema/spec. You can avoid installing @standard-schema/spec to save some bulk.

JsonSchema

Pre-bundled version of JsonSchema7 in @types/json-schema. You can avoid installing @types/json-schema to save some bulk.

Infer

Alias for StandardSchemaV1.Infer.

InferIn

Alias for StandardSchemaV1.InferInput.

Errors

Missing dependencies

Some schema providers require additional packages to convert to JsonSchema.

xsschema supports a lot of vendors, so they won't and shouldn't be set as a direct dependencies, which will lead to size growth even if you don't use them.

In a nutshell:

If you use zod v3: install zod-to-json-schema

If you use valibot: install @valibot/to-json-schema

All optional peer dependencies are noted in Coverage.

Unsupported schema vendor

This means that xsschema does not yet support this vendor.

If you are interested in adding support, please make sure the library meets the following requirements (choose one of two):

  • GitHub stars is greater than 100. (If the repo is not on GitHub, refer to the next entry)
  • npm package has over 1000 weekly downloads.
Edit on GitHub

Last updated on

On this page