json-schema-content-mapper

中文文档
0.1.0发布于 2026/11/04ISCTypeScriptES Module

A JSONSchema content mapper for TypeScript 9.1.

原始使用文档
文档目录7 节
  1. Features
  2. Installation
  3. Configuration ( tsconfig.json )
  4. Multi-File Schema References ( $ref )
  5. Example Setup
  6. Running Type Checking
  7. Technical Architecture

json-schema-content-mapper

A JSONSchema content mapper for TypeScript 9.1.

A native TypeScript 7.1+ Content Mapper plugin that dynamically compiles *.jsonschema files into fully-typed TypeScript sources on the fly using schema2dts.

No build-step generators or generic wildcard ambient declarations required. Your JSON Schemas are treated as first-class typed modules.

Features

  • Zero-Disk Overhead: Generates in-memory virtual .ts definition files compiled directly by tsc.
  • JSON-RPC Multiplexed: Built on top of vscode-jsonrpc to handle fast, concurrent, and non-blocking multi-project compilation contexts.
  • Seamless References: Supports multi-file architectures using standard JSON Schema $ref pointers.

Installation

Install the content mapper as a development dependency in your project:

npm install -D json-schema-content-mapper

Note: Ensure your workspace uses TypeScript 7.1 (or later) to support Content Mappers.


Configuration (tsconfig.json)

Register the package under the new contentMappers compiler property. We strongly recommend using the .jsonschema extension for your schema definition files.

{
  "compilerOptions": {
    "module": "preserve",
    "moduleResolution": "bundler",
    "strict": true
  },
  "contentMappers": [
    {
      "package": "json-schema-content-mapper",
      "extensions": [".jsonschema"],
      "options": {
        "exportNamespaces": true
      }
    }
  ],
  "include": ["src"]
}

Multi-File Schema References ($ref)

One of the major benefits of this content mapper is its ability to handle multi-file structures natively. Instead of bundling everything into a single giant schema, you can split your definitions across multiple .jsonschema files and reference them using JSON Pointers.

When a schema references another file via a $ref, the content mapper dynamically translates this link into a standard JavaScript/TypeScript import statement in the virtual .d.ts output. TypeScript's compiler then handles the cross-file dependency graph automatically.

Example Setup

1. Define a shared base schema (src/phone.jsonschema)

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "Phone",
  "type": "string",
  "pattern": "+[0-9]+.[0-9]+"
}

2. Reference it in your main schema (src/user.jsonschema)

Use the relative file path inside the $ref property:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "User",
  "type": "object",
  "properties": {
    "name": { "type": "string" },
    "email": { "type": "string" },
    "phone": { "$ref": "./common.jsonschema" }
  },
  "required": ["name"]
}

3. Consume naturally in your TypeScript code (src/index.ts)

TypeScript resolves the full chain. The generated type for User will automatically inherit the field (phone) from phone.jsonschema through a TypeScript intersection type:

import { User } from './user.jsonschema';

const user: User = {
  name: 'John Doe',
  email: '[email protected]',
  phone: '+33.16561655616',
};

console.log(user.id); // Available & Typed (string)
console.log(user.name); // Available & Typed (string)
console.log(user.phone); // Available & Typed (string)

Running Type Checking

Because Content Mappers invoke external code packages to compute module shapes, the TypeScript compiler requires an explicit security opt-in flag to run type-checking from your CLI:

npx tsc --runExternalCode --noEmit

Without the --runExternalCode flag, tsc will ignore the content mapper process for security reasons and throw an import resolution error.


Technical Architecture

This plugin establishes an asynchronous JSON-RPC 2.0 communication process pipeline with tsc via standard input/output streams (stdin/stdout).

  1. initialize: Negotiates structural formatting parameters (e.g., utf-16 character positioning).
  2. openProject: Caches incoming tsconfig.json plugin configurations into an isolated projectRegistry map linked to an opaque workspace projectHandle.
  3. transform: Extracts parameters, resolves the project context options, runs generateJSONSchemaTypes(), and delivers the compiled virtual code layer instantly back to the Go-compiled tsc core engine.
  4. closeProject: Clears references from the operational cache map to prevent memory leaks during watch modes.

Authors

License

ISC

版本与发布记录

json-schema-content-mapper 共发布 2 个版本,首次发布于 2026/11/04;过去 12 个月发布了 2 个版本。

版本发布日期距今
0.1.0latest2026/11/041 天前
0.0.0-stage2026/11/041 天前

关于 json-schema-content-mapper 的常见问题

json-schema-content-mapper 是什么?

A JSONSchema content mapper for TypeScript 9.1.

如何安装 json-schema-content-mapper?

在项目目录运行 npm install json-schema-content-mapper,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 json-schema-content-mapper,可以用 npx json-schema-content-mapper 直接运行。

json-schema-content-mapper 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

json-schema-content-mapper 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

json-schema-content-mapper 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=24.14.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

json-schema-content-mapper 可以商用吗?

json-schema-content-mapper 使用 ISC 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

json-schema-content-mapper 还在维护吗?

最新版本 0.1.0 发布于 2026/11/04(1 天前);至今共发布 2 个版本,其中过去 12 个月发布了 2 个;首次发布于 2026/11/04。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。