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

@lsolova/json-schema-validator

v0.2.4

Published

JSON schema validator (with Rust WASM based on jsonschema)

Readme

JSON schema validator

This is a JSON schema validating solution for browser and Node.js environments. It uses jsonschema Rust package wrapped into a WASM module and exposing functionality in SchemaValidator object.

Usage

SchemaValidator must be initialized first, by passing the deployed WASM file URL to SchemaValidator.init.

The SchemaValidator.validate is asynchronous and returns a Promise<void>. If error happens then an object is rejected, which contains field name - error pairs, or an object with an error variable.

type SchemaValidationError = {
    error: string | object
} | {
    // Key is field name, value is the validation error related to that field
    [key: string]: string
}

Browser

Copy the following file into your output directory: @lsolova/json-schema-validator/dist/assets/schema_validator.wasm.

Then a simple validate call can be used, by passing the content or the URL of the schema file (http:// and https:// protocols are supported) and the data to be validated. If an HTTP(S) schema is downloaded once, then it is cached until the SchemaValidator object exists.

Download is part of the normal network traffic via browser's fetch. This allows caching schemas by PWA, if implemented.

import { SchemaValidator } from "@lsolova/json-schema-validator";

async function initValidation {
    // Set the wasmURL to the URL of the exposed WASM file (see more details below)
    await SchemaValidator.init(wasmURL);
    // If there would be any schema to be registered, then it can be done after the
    // initialization, but before the first usage of that schema
    await SchemaValidator.registerSchema(uri, schema);
};

// The schema parameter could be a schema URI (either HTTP(S) or (ID)) or a schema definition
async function validate(schema, data) {
    await SchemaValidator.validate(schema, data);
};

This validator supports HTTP(S) references within the schema files (for example: https://example.com/schemas/my.json or see $ref in the schema example).

Schema example

{
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "type": "object",
    "properties": {
        "name": {"type": "string"},
        "email": {"$ref": "http://example.com/schemas/email.schema.json"}
    },
    "required": ["name", "email"]
}

Security

Though it does not use eval, there is a special Content Security Policy setting required by high security environments.

If any script-src is set in the Content-Security-Policy header, then a 'wasm-unsafe-eval' entry must be added into the script-src section of this header. (Details on MDN).

Content-Security-Policy: ...;script-src your entries 'wasm-unsafe-eval'; ...

Node.js

Thanks to WASM support of Node.js, this module can be used in Node.js environment too.

This is an ESM module. Therefore it can be imported only using import, require is not supported.

It should be initialized by loading the WASM module first, before any validation.

import { readFile } from "node:fs/promises";
import { SchemaValidator } from "@lsolova/json-schema-validator";

async function sample() {
    const wasmBuffer = await readFile(
        "./node_modules/@lsolova/json-schema-validator/dist/assets/schema_validator.wasm"
    );
    await SchemaValidator.init(wasmBuffer);

    const schemaContent = await readFile("./schemas/my.schema.json", { encoding: "utf8" });
    await SchemaValidator.validate(schemaContent, myContentToValidate);
}

How to add WASM file to your deployment?

Browser: Using esbuild

TBD

Browser: Using Vite

Please, use the explicit URL import feature of Vite passing WASM URL to the SchemaValidator.init function. Everything else will be managed by Vite build.

import { SchemaValidator } from "@lsolova/json-schema-validator";
import wasmURL from "@lsolova/json-schema-validator/dist/assets/schema_validator.wasm?url";

async function initValidation() {
    await SchemaValidator.init(wasmURL);
}

Node.js: Using Node functions

The SchemaValidator.init supports passing a module instead of a path. Therefore WASM can be read from file and passed to the init function.

import { readFile } from "node:fs/promises";
import { SchemaValidator } from "@lsolova/json-schema-validator";

async function sample() {
    const wasmBuffer = await fs.readFile(
        "./node_modules/@lsolova/json-schema-validator/dist/assets/schema_validator.wasm"
    );
    await SchemaValidator.init(wasmBuffer);
}

Development

  1. Run npm i
  2. Run npm run build

Local testing

To start the web application on localhost, run npm run serve.

To check the Node.js run on localhost, run npx tsx node-example.ts.