npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@sozai/schema

v0.1.6

Published

JSON schema validation

Readme

@sozai/schema

JSON Schema validation and FromSchema type generation.

Installation

pnpm add @sozai/schema

Usage

import type { Schema, FromSchema } from '@sozai/schema'
import { createValidator, isType, ValidationError } from '@sozai/schema'

// Define a schema — the single source of truth for the shape
const userSchema = {
  type: 'object',
  properties: {
    name: { type: 'string' },
    age: { type: 'number', minimum: 18 },
  },
  required: ['name'],
  additionalProperties: false,
} as const satisfies Schema

// Derive the TypeScript type — no duplication
type User = FromSchema<typeof userSchema>

// Build a reusable validator
const validateUser = createValidator<typeof userSchema, User>(userSchema)

const raw: unknown = JSON.parse('{"name":"Ada","age":36}')
if (isType(validateUser, raw)) {
  console.log(raw.name) // `raw` is narrowed to User
}

const result = validateUser({ name: 'bad', age: 10 })
if (result instanceof ValidationError) {
  for (const issue of result.issues) {
    console.log(issue.path.join('.'), issue.message)
  }
}

For schemas that arrive at runtime, createValidatorFactory() owns an isolated AJV instance: dispose() drops the instance and every validator it memoised, which the shared instances behind createValidator keep for the life of the process. Validators already returned keep working and keep the instance alive until they are dropped too.

createValidatorCache() builds on it: a bounded LRU of validators over factories that are disposed and replaced after maxCompiles distinct compiles. Schemas equal up to key order share one compile, and a failed compile is cached. dispose() is terminal: later get calls throw, and disposed reports it.

import { createValidatorCache } from '@sozai/schema'

const cache = createValidatorCache({ factory: { strict: false, logger: false } })

const validate = cache.get({ type: 'string', minLength: 1 })
cache.stats() // { generation: 0, compiles: 1, entries: 1 }
  • A validator checks a snapshot of the schema, so ValidationError.schema is that snapshot (equal to, not the same object as, the input) and its issues follow sorted key order.
  • Schemas must be self-contained: every compile leaves the instance as it found it, so $ref to another schema's $id does not resolve. A $ref to a location inside the same schema works.
  • A root $id equal to a meta-schema id is rejected with Schema $id <id> is reserved.
  • A boolean schema, cast to Schema, is accepted by createValidator, the factory and the cache.

Look a validator up at the moment of use, and do not hold it longer than needed. A long-lived holder, such as a suspended run keeping a validator, keeps its disposed AJV instance alive.

Also provides assertType, asType, createStandardValidator, resolveSchema, and more — see the schema reference (part of the sozai:validation skill) for the full API.