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

@tse-ian/jsonschema-napi-rs

v1.0.7

Published

Template project for writing node package with napi-rs

Downloads

13

Readme

jsonschema-napi-rs

基于 napi-rs 封装 Rust 的 jsonschema crate,为 Node/TypeScript 提供高性能的 JSON Schema 校验能力。

特性

  • 一次性校验:isValid(schema, instance)
  • 复用校验器:validatorFor(schema) 返回 Validator 以复用编译后的 schema
  • 详细输出:
    • Validator.isValid(instance):布尔校验
    • Validator.iterErrors(instance):错误列表(message、instancePath、schemaLocation、evaluationPath)
  • 自动生成 TypeScript 类型定义,直接在 TS 项目中使用

安装与本地构建

  • 安装依赖:yarn
  • 本地构建:yarn build
    • 构建后将生成平台对应的 .node 文件,index.js 会自动按平台加载

快速使用

npm install @tse-ian/jsonschema-napi-rs@latest
const { isValid, validatorFor } = require('@tse-ian/jsonschema-napi-rs')

const schema = {
  type: 'object',
  properties: {
    a: { type: 'number' },
  },
  required: ['a'],
  additionalProperties: false,
}

// 一次性校验
console.log(isValid(schema, { a: 1 })) // true

// 复用校验器
const v = validatorFor(schema)
console.log(v.isValid({ a: 2 })) // true
console.log(v.iterErrors({ a: 'x' }))
// [ { message, instancePath, schemaLocation, evaluationPath }, ... ]

TypeScript 类型(自动生成)见 index.d.ts

export interface ValidationErrorOut {
  message: string
  instancePath: string
  schemaLocation: string
  evaluationPath: string
}

export declare class Validator {
  isValid(instance: any): boolean
  iterErrors(instance: any): Array<ValidationErrorOut>
}

export declare function isValid(schema: any, instance: any): boolean
export declare function validatorFor(schema: any): Validator

性能对比

以下对比来自本仓库的基准测试(详见 benchmark/bench.ts),展示原生实现与纯 JavaScript 的差异:

│ (index) │ Task name                       │ Latency avg (ns) │ Latency med (ns) │ Throughput avg (ops/s) │ Throughput med (ops/s) │ Samples │
├─────────┼─────────────────────────────────┼──────────────────┼──────────────────┼────────────────────────┼────────────────────────┼─────────┤
│ 0       │ 'Native validator.isValid'      │ '15805 ± 7.38%'  │ '13796 ± 498.00' │ '68601 ± 0.07%'        │ '72485 ± 2686'         │ 126542  │
│ 1       │ 'JavaScript Validator.validate' │ '38220 ± 0.63%'  │ '34417 ± 855.00' │ '27453 ± 0.11%'        │ '29055 ± 736'          │ 52330   │
└─────────┴─────────────────────────────────┴──────────────────┴──────────────────┴────────────────────────┴────────────────────────┴─────────┘

在多次校验场景下,使用 validatorFor 复用已编译的 schema 可进一步提升性能与吞吐。

运行基准测试

  • 运行命令:yarn bench
  • 脚本位置:benchmark/bench.ts

开发脚本

  • 构建:yarn build(发布构建) / yarn build:debug(调试)
  • 格式化:yarn format
  • 代码检查:yarn lint

关键文件

许可证

MIT